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 15a. 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);
}
}