Application.Current.Shutdown() 不会

发布于 2024-08-14 04:00:05 字数 182 浏览 3 评论 0原文

标题就是关于这个的。 WPF 应用程序,带有一些用于 IPC 的 WCF 内容。我调用 Application.Current.Shutdown() 并且应用程序继续正常运行。我认为 Shutdown 应该是不可阻挡的。

也许是因为它是从后台线程调用的?我需要做一些调度员的摆弄吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

俏︾媚 2024-08-21 04:00:05

当我在主线程以外的任何线程中调用 Application.Current.Shutdown 时,您都会遇到异常,因此我假设您已经正确使用了“调度程序摆弄”。

无论如何,这都会编译并退出应用程序,因此如果调度程序位看起来不像您所拥有的那样,您可以将其放入:

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

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();
岛歌少女 2024-08-21 04:00:05

根据我的经验,所有线程都必须显式终止或标记为后台线程才能关闭应用程序。

下面是在后台启动读取线程的示例:

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

IsBackground 属性是关键。如果没有设置,当您调用 Shutdown 时,线程将不会终止。

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.

初见你 2024-08-21 04:00:05

我仅在从 Visual Studio 运行时遇到 Application.Current.Shutdown 无法工作的情况。在 Visual Studio 中(至少是我使用的 2010 版本)Application.Current.Shutdown 不执行任何操作。如果我单步执行,它会执行这一行,然后继续。如果我从 Windows 资源管理器运行该程序(作为 .exe),则 Application.Current.Shutdown 工作正常。

可能对此有一个解释,因为在调试期间其他线程处于活动状态,但我无法解释它。

I only experience Application.Current.Shutdown not working when I'm running from Visual Studio. Within Visual Studio (at least the 2010 version I'm using) Application.Current.Shutdown doesn't do a thing. If I single step through it executes this line and then continues. If I run the program (as an .exe) from Windows Explorer then Application.Current.Shutdown works fine.

There is probably an explanation for this since during debug other threads are active, but I can't explain it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文