Brief
When working with a multithreading application it is very important for developers to handle multiple threads for a critical section of code.Monitor and lock is the way to provide thread safety in a multithreaded application in C#. Both provide a mechanism to ensure that only one thread is executing code at the same time to avoid any functional breaking of code.
Example
Lock for only one request to be processed at a time.private static object s_SyncRoot = new object();
...
new Task(() =>
{
lock (s_SyncRoot)
{
// process payment - single thread
}
}).Start();
return "ok";
Comments
Post a Comment