5

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

Damian
  • 2,709
  • 2
  • 29
  • 40
  • What happens if you call: `Dispatcher.Run()` on the 2nd thread after it was previously shutdown? – Mike Dinescu May 30 '14 at 15:47
  • 1
    Also, it might help if you could include just the bare minimum code to repo your issue. This way we could verify that a solution works before posting an answer.. – Mike Dinescu May 30 '14 at 15:50
  • Thats precisely what I am doing and it throw an `InvalidOperationException` with the following description: Additional information: "Cannot perform requested operation because the Dispatcher shut down." – Damian May 30 '14 at 15:50
  • Added a gist with most of the relevant code: https://gist.github.com/DamianReeves/76771a031f05a8be042d – Damian May 30 '14 at 15:58
  • 1
    Have you read this? http://reedcopsey.com/2011/11/28/launching-a-wpf-window-in-a-separate-thread-part-1/ I've used it successfully in the past. – Mike Dinescu May 30 '14 at 17:07
  • The issue mentioned is kind of the opposite of what I'm facing (my code shutsdown the dispatcher, his code wasn't and he is forcing it). That being said, I'll take a closer look at how my use of the 'EventLoopScheduler` is affecting the behavior of the second thread. – Damian May 30 '14 at 17:40
  • This might be a dumb question, but have you tried Dispatcher.CurrentDispatcher which should create a new Dispatcher for the calling thread. What I'm unsure of is whether a shutdown Dispatcher will be replaced... – Zache Jun 12 '14 at 08:05
  • I have, and it doesn't seem to be replaced – Damian Jun 12 '14 at 12:58
  • What about just hiding the window instead of closing it? You said you plan on showing it again later on anyways. – Rachel Jun 12 '14 at 14:12

1 Answers1

1

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.

Michael Shaffer
  • 374
  • 2
  • 16