What important difference exists between Monitor.TryEnter(object) And Monitor.TryEnter(object, ref bool)?

前端 未结 1 949
无人共我
无人共我 2021-02-03 11:48

It seems that these code snippets ought to behave identically:

1: Monitor.TryEnter(object)

if (Monitor.TryEnter(lockObject))
{
    try
    {
        Do         


        
相关标签:
1条回答
  • 2021-02-03 12:11
    1. Monitor.TryEnter could succeed and then an asynchroneous exception like ThreadAbortException or OutOfMemoryException (that can happen without visible allocations) is triggered. Then the lock would be taken but never released.

    See: Locks and exceptions do not mix

    0 讨论(0)
提交回复
热议问题