Moniter class used for locking data with synchronization - Csharp.Net

Q.  For locking the data with synchronization which class will be used?
- Published on 31 Aug 15

a. Lock
b. Moniter
c. SyncLock
d. Deadlock

ANSWER: Moniter
 
By using Moniter class a block of code can be access by one thread at a time. Moniter.Enter() method allows only one thread to access the resource.

Example:
public void ShowNumbers()
{
Monitor.Enter(this);
try
{
for (int i = 0; i < 5; i++)
{
Thread.Sleep(100);
Console.Write(i + ",");
}
Console.WriteLine();
}
finally
{
Monitor.Exit(this);
}
}

Post your comment / Share knowledge


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)