Lock on static object from within a delegate is not working

ⅰ亾dé卋堺 提交于 2019-12-06 03:48:12

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!