BikingGlobetrotter
BikingGlobetrotter

Reputation: 174

Thread.Join() causes processing of window messages in WPF application

I found some strange behavior in WPF Dispatcher mechanism. Let me quickly explain the application:

I have a WPF window with several OpenTK OpenGLControls hosted inside WindowsFormsHosts. During the OnPaint event of an OpenGLControl, I do the rendering of the corresponding 3D scene.

During rendering it may happen that I need to wait for another thread to finish. The problem now is that during the call of Thread.Join(), the Dispatcher processes for other window messages for whatever reasons. This is causing problems in my application and I want to disable / prevent / understand the behavior. Do you know how this is possible?

In my application, this causes an unexpected OpenGl context switch as the paint event of a different OpenGl context is processed while processing the paint event of another OpenGL Control.

This is the callstack of the application, maybe this helps: Callstack http://public.virtualmischa.de/bugs/paintevent-while-join.png

Thank you

Michael

Upvotes: 1

Views: 756

Answers (1)

noseratio
noseratio

Reputation: 61736

Thread.Join is known to pump more messages than WaitHandle.WaitOne normally does.

Instead of:

_thread.Join();

Try this:

Task.Run(() => _thread.Join()).Wait();

This will move _thread.Join() from the UI thread to a ThreadPool thread, where there's nothing to pump. It should prevent pumping on the UI thread.

It's possible to use timeout, too:

Task.Run(() => _thread.Join(timeout)).Wait();

Upvotes: 1

Related Questions