在 WPF 中 App.Current.SessionEnding 必须在几秒钟内返回,否则会出现“应用程序不响应”窗口。所以不能在这个事件处理程序中要求用户保存他的数据,因为用户的响应时间超过几秒钟。
我认为一个解决方案是取消注销/关闭/重新启动,并在用户回答文件保存对话框时恢复它。
ReasonSessionEnding _reasonSessionEnding;
App.Current.SessionEnding +=
new SessionEndingCancelEventHandler(Current_SessionEnding);
void Current_SessionEnding(object sender, SessionEndingCancelEventArgs e)
{
if (_dataModified)
{
e.Cancel = true;
_reasonSessionEnding = e.ReasonSessionEnding;
Dispatcher.CurrentDispatcher.BeginInvoke(new Action(EndSession));
}
}
void EndSession()
{
if (SaveWithConfirmation()) // if the user didn't press Cancel
//if (_reasonSessionEnding = ReasonSessionEnding.Logoff)
// logoff
//else
// shutdown or restart ?
}
问题是 ReasonSessionEnding 没有告诉我 Windows 是关闭还是重新启动(它不区分两者)。
那么,我的程序在会话结束事件上应该做什么?它甚至应该做任何事情,还是在这个事件上什么都不做是标准?
要求用户将他的更改保存在我的主窗体的 OnClosing 方法中,因此他不会丢失数据,但我认为“应用程序不响应”窗口并不表示正常的工作流程。
我猜不希望取消关闭,因为其他一些程序已经关闭。