Do I need to call Close() on a ManualResetEvent?

前端 未结 6 943
伪装坚强ぢ
伪装坚强ぢ 2020-12-10 11:43

I\'ve been reading up on .NET Threading and was working on some code that uses a ManualResetEvent. I have found lots of code samples on the internet. However, when reading

6条回答
  •  失恋的感觉
    2020-12-10 12:26

    You'll notice the code

     using (var mre = new ManualResetEvent(false))
     {
        // Process the left child asynchronously
        ThreadPool.QueueUserWorkItem(delegate
        {
            Process(tree.Left, action);
            mre.Set();
        });
    
        // Process current node and right child synchronously
        action(tree.Data);
        Process(tree.Right, action);
    
        // Wait for the left child
        mre.WaitOne();
    }
    

    uses the 'using' keyword. This automatically calls the dispose method when finished even if the code throws an exception.

提交回复
热议问题