C# manual lock/unlock

前端 未结 5 2221
没有蜡笔的小新
没有蜡笔的小新 2020-12-01 08:25

I have a function in C# that can be called multiple times from multiple threads and I want it to be done only once so I thought about this:

class MyClass
{
          


        
5条回答
  •  渐次进展
    2020-12-01 08:29

    The lock keyword is just syntactic sugar for the Monitor class. Also you could call Monitor.Enter(), Monitor.Exit().

    But the Monitor class itself has also the functions TryEnter() and Wait() which could help in your situation.

提交回复
热议问题