开发者

Does Monitor.Enter() allow events to be processed if called on the UI thread of a C# application?

开发者 https://www.devze.com 2023-02-10 16:12 出处:网络
I\'m investigating a stack trace (seen while debugging) that looks something like the following: MyUtility.AcquireLock()

I'm investigating a stack trace (seen while debugging) that looks something like the following:

MyUtility.AcquireLock()
[some of our code]
MyControl.OnPaint(PaintEventArgs e)
[some external WinForms (painting) code]
(native code corresponding to Monitor.Enter(object))
MyUtility.AcquireLock()
[some of our code]
MyControl.OnLoad(EventArgs e)
开发者_如何转开发

Does this make sense or did something screw up my stack trace?

I'm almost (but not completely) certain, no other thread is holding this lock, I'm trying to acquire (by using Monitor.Enter()).


It does. Monitor.Enter does a so called alertable wait. Only few people know what this means in 2011 because it is an obscure concept. It means that the thread will pump window messages while waiting. Yes, this seems grotesque. You are currently in a very dark world of pain because you are facing nondeterministic reentrancy issues.

The way to the light is to not wait on the gui thread at all! Start a background thread, maybe a BackgroundWorker.

0

精彩评论

暂无评论...
验证码 换一张
取 消