简体   繁体   English

从另一个线程关闭主应用程序

[英]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. 我正在开发一个由c#+ HTML + Java Script编写的应用程序,其结构如下:基本窗口是一个WPF MainWindow类,它打开一个创建网页的线程。 This thread handles all the UI events. 该线程处理所有UI事件。

The WebBrowser XAML code is: WebBrowser XAML代码是:

    <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. 问题是我想从HTML线程关闭应用程序。 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). 我不能使用App.Current.ShutDown()因为我不在主UI线程中(我试过这样做但是有一个例外)。

(BTW: For who knows PhoneGap: my application structure is very similar to how the Win8 PhoneGap is built.) (顺便说一句:谁知道PhoneGap:我的应用程序结构与Win8 PhoneGap的构建方式非常相似。)

Can anyone please help me? 谁能帮帮我吗? Thanks. 谢谢。

What you want is (edited and taken from Application.Current.Shutdown() doesn't ) 你想要的是(编辑并从Application.Current.Shutdown()中获取

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 请参阅http://social.msdn.microsoft.com/Forums/vstudio/de-DE/68bea444-1fa7-4948-8b8c-bf61fbc6dc2b/how-to-exit-a-wpf-application

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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