views:

217

answers:

2

Title's about it. WPF app with some WCF stuff for IPC. I call Application.Current.Shutdown() and the app continues on happily. I thought Shutdown was supposed to be unstoppable.

Perhaps because it's being called from a background thread? Do I need to do some dispatcher fiddling?

A: 

In my experience all threads have to either be terminated explicitly or be marked as background threads in order for the application to close.

Here is an example of kicking off a read thread in the background:

_readThread = new Thread(new ThreadStart(ReadThread));
_readThread.Name = "Receiver";
_readThread.Priority = ThreadPriority.Highest;
_readThread.IsBackground = true;
_readThread.Start();

The IsBackground property is the key. Without that being set, the thread won't terminate on when you call Shutdown.

Gabe
The app closes normally through other means though.
Tom
A: 

You get an exception when I call Application.Current.Shutdown in any thread other than the main one, so I'd assume you where using "dispatcher fiddling" properly already.

In any case, this compiles and quits an application, so if the dispatcher bit doesn't look like what you have you could sling it in:

ThreadStart ts = delegate()
    {
        Dispatcher.BeginInvoke((Action)delegate()
        {
            Application.Current.Shutdown();
        });
     };
     Thread t = new Thread(ts);
     t.Start();
MoominTroll
Similar to what I was throwing in before I got sidetracked with other stuff. Thanks for verifying.
Tom