处理WPF退出事件 [英] Handle a WPF Exit Event

查看:2451
本文介绍了处理WPF退出事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有办法处理一个WPF应用程序退出事件,使退出被取消。使用案例是,我有一个客户端 - 服务器情况,其中服务器是一个WPF应用程序。我想让WPF应用程序通知客户端当它正在关闭,但取消关机过程。

I was wondering if there was a way to handle a WPF Application Exit event such that the exit was cancelled. The use case is that I have a client-server situation where the server is a WPF app. I want the WPF app to notify the client when it is shutting down, but cancel the shutdown process. The client should receive the shutdown notification, do its own required cleanup and then ask the WPF app to shutdown via a request.

这是否可能?

推荐答案

您可以像这样在主窗口挂钩事件Closing

You can hook the event Closing on your main window like this -

<Window Closing="Window_Closing">

在您的事件中将e.Cancel设置为true以停止窗口关闭。在你的情况下,你可以维护一些字段,这将设置一旦你收到客户端的通知,他做了cleanUp,现在安全关闭窗口。只需将该值设置为e.Cancel

And in your event set the e.Cancel to true to stop the window from closing. In your case you can maintain some field which will be set once you get notification from client that he's done with cleanUp and its safe now to close the window. Simply set that value to e.Cancel

private void Window_Closing(object sender, CancelEventArgs e)
{
   e.Cancel = true;
}

这篇关于处理WPF退出事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆