Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding a new Dispatcher to a thread in WPF

I have a situation where I am showing a Window which acts as a splash screen. This window is created on a separate thread from the main ui thread and as such it is associated with its own Dispatcher (i.e. I end up with 2 Dispatchers the Main Ui dispatcher and the "Splash" Dispatcher).

When I close the splash window the Dispatcher associated with the splash window's thread shuts down (though the thread is still running, which is what I want); however, I would like to be able to show the splash window again at a later time on the same thread. The problem I face is the second time I try and do this the Dispatcher complains that it has been shut down.

Is there a way to force a new Dispatcher to be associated with a thread which previously had a Dispatcher associated with it?

Or is there a way to cause the Dispatcher not to shutdown when the window is closed?

I am aware I could solve this issue by creating a new thread, but I really would prefer not to do this. Ideally I want to have one dedicated thread which is responsible for out of band notifications like the splash and popup "toasters".

NOTE: I've posted the relevant code at this gist: https://gist.github.com/DamianReeves/76771a031f05a8be042d

like image 583
Damian Avatar asked Nov 10 '22 07:11

Damian


1 Answers

To answer your first question: Is there a way to force a new Dispatcher to be associated with a thread which previously had a Dispatcher associated with it?

"When a Dispatcher is created on a thread, it becomes the only Dispatcher that can be associated with the thread, even if the Dispatcher is shut down."

also

"If a Dispatcher is shut down, it cannot be restarted."

http://msdn.microsoft.com/en-us/library/vstudio/system.windows.threading.dispatcher in the Remarks section.

like image 198
Michael Shaffer Avatar answered Nov 14 '22 21:11

Michael Shaffer