Method lock in c#

后端 未结 4 1991
孤城傲影
孤城傲影 2021-02-19 05:16

I have one class with these three methods. This class is used by many threads. I would like the Method1 to wait, if Method2 and/or Method3 are running in any threads. Any sugges

4条回答
  •  忘掉有多难
    2021-02-19 05:58

    If you are multi-threading then the lock has to be accessible to all threads. Therefore, in this case, your locks needs to be static for the static methods to see it.

    Your current setup will make a new lock object for each thread. Therefore, providing now synchronization.

提交回复
热议问题