简体   繁体   中英

Lock on static object from within a delegate is not working

How come this isn't working?

private static object Lock_HandleError = new object();
public static void HandleError(Exception ex)
{
    lock( Lock_HandleError )
    {
        //IF the UI is processing a visual tree event (such as IsVisibleChanged), it throws an exception when showing a MessageBox as described here: http://social.msdn.microsoft.com/forums/en-US/wpf/thread/44962927-006e-4629-9aa3-100357861442
        //The solution is to dispatch and queue the MessageBox. We must use BeginInvoke because dispatcher processing is suspended in such cases.
        Dispatcher.CurrentDispatcher.BeginInvoke((Action)delegate()
        {
            lock( Lock_HandleError )
            {
                Dispatcher.CurrentDispatcher.BeginInvoke((Action)delegate(){ 
                    HandleError(new Exception("testing purposes only")); 
                }, DispatcherPriority.Background);

                MessageBox.Show(ex.Message, "Application Error", MessageBoxButton.OK, MessageBoxImage.Error);
                //This point is not reached until the user clicks "OK"
            }
        }, DispatcherPriority.Background);
    }
}

public void main()
{
    HandleError(new Exception("The first error"));
}

The expected behaviour of the above code is that one error message will appear at a time, and when the user clicks "OK", the on the Lock_HandleError object will get released from the dispatched thread, and the next call to HandleError can proceed - but what I'm getting is an infinite cascade of error messages without ever getting to hit "OK".

Why isn't this lock working?

By setting breakpoints at the entry and exit of each lock statement, I can clearly see that the delegate is calling lock() and dispatching the a new call to "HandleError" again and then pausing on MessageBox to wait for user input.

Meanwhile, in another thread, that dispatched call to HandleError gets run - but instead of waiting at the lock() statement like it should, it's just blasting through it even though the MessageBox delegate clearly placed a lock and has not yet released it.

Two part answer:

  1. Understand that locks are re-entrant. When a thread already holds a lock on an object, that thread can take the same lock again and again without blocking.

  2. While the first MessageBox is up, the UI thread is still pumping messages, so subsequent (recursive) calls to HandleError are being processed on the UI thread (which, because it already holds the lock, can re-enter it).

Why isn't this lock working?

A thread is allowed to enter a lock statement which it already owns. In essense, lock doesn't block its own thread.

As such, what's happening is the original thread takes the lock, and then is allowed to add messages to the Dispatcher's Queue. It can add as many as it wants.

The Dispatcher, when processing, gets the first message, and then calls HandleError. Since this is running in the dispatcher thread, it's allowed to enter the outer and inner lock, and call HandleError again, recursively adding new messages to the queue in an endless loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM