locking a resource via lock within try. Is it wrong?

前端 未结 3 982
走了就别回头了
走了就别回头了 2021-02-05 05:00

Is there anything wrong with using lock with a try block? I remember reading somewhere that we should always try to put minimum amount of code within try block and lock itself i

3条回答
  •  说谎
    说谎 (楼主)
    2021-02-05 05:19

    you can always use the longer syntax like this:

    System.Threading.Monitor.Enter(x);
    try {
       ...
    }
    catch(Exception e)
    {
    }
    finally {
       System.Threading.Monitor.Exit(x);
    }
    

提交回复
热议问题