Skip to content

ErcanOPAK.com

  • ASP.Net WebForms
  • ASP.Net MVC
  • C#
  • SQL
  • MySQL
  • PHP
  • Devexpress
  • Reportviewer
  • About
C#

How to lock the ciritical code part in C#

- 19.03.22 - ErcanOPAK

According to Microsoft: The lock keyword ensures that one thread does not enter a critical section of code while another thread is in the critical section. If another thread tries to enter a locked code, it will wait, block, until the object is released.

The lock keyword calls Enter at the start of the block and Exit at the end of the block. lock keyword actually handles Monitor class at the back end.

It is so easy to do it. Here is an example:

private static readonly Object obj = new Object();

lock (obj)
{
    // critical section
}

In the above code, first, the thread enters a critical section, and then it will lock obj. When another thread tries to enter, it will also try to lock obj, which is already locked by the first thread. The second thread will have to wait for the first thread to release obj. When the first thread leaves, then another thread will lock obj and will enter the critical section.

Related posts:

How to get value from resx file in C#
Fire Combobox SelectedIndexChanged with button code-behind
Formatting a Nullable DateTime with ToString() in ASP.Net MVC
What is the difference between 'ref' and 'out' keywords in C#
Post Views: 25

Post navigation

How to make theater mode the default for Youtube
How to get value from resx file in C#

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

December 2023
M T W T F S S
 123
45678910
11121314151617
18192021222324
25262728293031
« Nov    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (702)
  • Get the First and Last Word from a String or Sentence in SQL (697)
  • How to select distinct rows in a datatable in C# (559)
  • Add Constraint to SQL Table to ensure email contains @ (455)
  • Average of all values in a column that are not zero in SQL (372)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (343)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (338)
  • Find numbers with more than two decimal places in SQL (323)
  • Confirm before process with ASPxButton in Devexpress (316)
  • ASPxGridView – Disable CheckBox based on condition in GridViewCommandColumn (292)

Recent Posts

  • How to find a specific text string in a SQL Server Stored Procedure, Function, View or Trigger
  • How to convert JPG or PNG to WebP in batch or single mode
  • How to get the first and the last day of previous month in SQL Server
  • Not Null check on LEFT function with T-SQL
  • NICE 100 YILLARA TÜRKİYEM
  • How to delete all commit history in github
  • How to display HTML components on the same line in CSS
  • How to insert results of a stored procedure into a temporary table
  • How to remove all non alphanumeric characters from a string in C#
  • How to get the Xth Day of the Week of the Year in C#

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (702)
  • Get the First and Last Word from a String or Sentence in SQL (697)
  • How to select distinct rows in a datatable in C# (559)
  • Add Constraint to SQL Table to ensure email contains @ (455)
  • Average of all values in a column that are not zero in SQL (372)

Recent Posts

  • How to find a specific text string in a SQL Server Stored Procedure, Function, View or Trigger
  • How to convert JPG or PNG to WebP in batch or single mode
  • How to get the first and the last day of previous month in SQL Server
  • Not Null check on LEFT function with T-SQL
  • NICE 100 YILLARA TÜRKİYEM

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter

© 2023 ErcanOPAK.com

Proudly powered by WordPress | Theme: Xblog Plus by wpthemespace.com