简体   繁体   中英

Close the main application from a different thread

I am developing an application which is written by c# + HTML + Java Script in the following structure: the base window is a WPF MainWindow class that opens a thread which creates a web page. This thread handles all the UI events.

The WebBrowser XAML code is:

    <WebBrowser x:Name="CordovaBrowser" 
        Opacity="0"
        HorizontalAlignment="Stretch"  
        VerticalAlignment="Stretch" 
        Loaded="CordovaBrowser_Loaded" 
        Unloaded="CordovaBrowser_Unloaded"   
        LoadCompleted="CordovaBrowser_LoadCompleted" 
     />

The problem is that I want to close the application from the HTML thread. I cannot use App.Current.ShutDown() because I'm not in the main UI thread (I've tried to do so but got an exception).

(BTW: For who knows PhoneGap: my application structure is very similar to how the Win8 PhoneGap is built.)

Can anyone please help me? Thanks.

What you want is (edited and taken from Application.Current.Shutdown() doesn't )

ThreadStart ts = delegate()
{
    Dispatcher.BeginInvoke((Action)delegate()
    {
        Application.Current.Shutdown();
    });
 };
 Thread t = new Thread(ts);
 t.Start();

See http://social.msdn.microsoft.com/Forums/vstudio/de-DE/68bea444-1fa7-4948-8b8c-bf61fbc6dc2b/how-to-exit-a-wpf-application

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM