9

OK, I should start this off by saying I'm not sure this is necessarily the right way to deal with this but...

Basically I have created a Window in WPF for displaying notifications in my application (a bit like Outlook New Mail notification). I would like to try and show this Window in it's own thread (it might do some work in the future).

I've created the Window using WPF because it's a bit nicer at handling things like AlwaysOnTop and Fading In and Out.

The application that shows the notification is a 3.5 Windows Forms App. I've seen examples similar to this SOF: C# winforms startup (Splash) form not hiding for showing a Form in a different thread however I'm unable to start a new Message Loop with the WPF Window.

I've tried just calling Notification.Show() in a new thread however the Window never displays.

  1. Is it possible to show this WPF in it's own thread?

  2. Why do I see some resources say that you should not show any UI in a separate thread?

Community
  • 1
  • 1
MrEdmundo
  • 5,105
  • 13
  • 46
  • 58

3 Answers3

16

The WPF Threading Model has details on this. Look for "Multiple Windows, Multiple Threads."

Basically, you start a new thread, which creates the relevant window and then calls

System.Windows.Threading.Dispatcher.Run();

in order to run a dispatcher for that window, on the new thread.

Pang
  • 9,564
  • 146
  • 81
  • 122
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Thanks, this line: System.Windows.Threading.Dispatcher.Run() makes my form show in the Thread. Don't fully understand it yet but the article gives me a direction, thanks. Interestingly the article says it is perfectly reasonable to have UI forms created on different threads. – MrEdmundo Apr 08 '09 at 12:05
  • +1 not calling `Dipatcher.run` was my problem as well, without it my new window would flash and quickly disappear. – Drahcir Jul 30 '13 at 17:00
  • 1
    @MikeCole: Dispatcher.InvokeShutdown? – Jon Skeet Aug 02 '13 at 21:05
  • The link is not available anymore. Do you know its new url? – Zhr Saghaie Jun 18 '15 at 07:28
  • 1
    @ayssa: It's odd - the Google cached version works, but the original doesn't. I've included both in the answer now. (I would be tempted to delete this answer now as it's basically just a link, but it's been accepted. Will edit to give more information.) – Jon Skeet Jun 18 '15 at 07:38
  • You can add a link to a snapshot of the google cached version!! – Zhr Saghaie Jun 18 '15 at 07:45
  • @ayssa: Well I added a link to the Google cached version - I don't think I want to start taking a separate copy of that to host it myself... – Jon Skeet Jun 18 '15 at 07:46
  • what about [web archive](https://web.archive.org/web/20140206152647/http://msdn.microsoft.com/en-us/library/ms741870(v=vs.110).aspx) ? – Zhr Saghaie Jun 18 '15 at 08:41
  • 1
    @ayssa: I'm hoping this is a temporary blip... although with the extra information in the answer now, the link is less important. – Jon Skeet Jun 18 '15 at 08:54
3
  1. Is it possible to show this WPF in it's own thread?

Absolutely. Just create a new STA thread and create and display your Window from it. I've used this technique for a splash screen because the main (UI) thread got really tied up doing other things. It was completely out of my control how much work was being done on the UI thread because of a 3rd party framework we were using.

  1. Why do I see some resources say that you should not show any UI in a separate thread?

Possibly because of the extra complexity involved. If one of your Windows wants to pass data to another, you need to remember that they're executing on separate threads. Therefore, each should use its Dispatcher to marshal calls to the correct thread.

Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
  • Hi, I've tried the new Window in a STA thread, however when I call Notification.Show() nothing is displayed. – MrEdmundo Apr 08 '09 at 11:51
  • 1
    You may need to simplify and post minimal code. It may be something simple like forgetting to start the thread ;) – Kent Boogaart Apr 08 '09 at 12:22
0

You should display the notification using the main UI thread and if that window is going to perform any work, run that work in a background thread. You should not use more than one thread to access or create UI objects, not even if you don't share those objects between threads.

Gerco Dries
  • 6,682
  • 1
  • 26
  • 35
  • Fair enough, but why? What I mean by Why is, Why should I not create UI objects in more than one thread? I want this window to load and display without affecting the users current work – MrEdmundo Apr 08 '09 at 11:39