我有一个适用于 Windows 的 C#/xaml 程序。当用户使用屏幕右上角的“x”关闭程序时,我需要运行一个函数。我几乎尝试了所有方法,但似乎无法弄清楚为什么它不起作用。以下是我目前无法使用的一些代码:
public partial class MainWindow : Window {
bool shutdown = false;
public MainWindow() {
InitializeComponent();
OutputManagerThread.Start();
AppDomain.CurrentDomain.ProcessExit += new EventHandler(OnExit);
}
void OnExit(object sender,EventArgs e) {
shutdown = true;
}
}
为什么这个 OnExit 函数什么都不做。我无法从中打印或执行该函数内的任何其他代码。
谢谢
最佳答案
这不是正确的事件。你要Window.Closing .
The Closing event is raised when Close is called, if a window's Close button is clicked, or if the user presses ALT+F4.
关于c# - C# 程序退出时的运行函数无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51310048/