当应用程序有未保存的数据时,处理注销/关闭/重新启动的正确方法是什么?
在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 方法中保存他的更改,这样他就不会丢失数据,但我认为“应用程序没有响应”窗口并不表明正常的工作流程。
我想取消关闭并不是所希望的,因为其他一些程序已经被关闭了。
In WPF App.Current.SessionEnding must return in a few seconds, otherwise the "application does not respond" window appears. So the user can't be asked in this event handler to save his data, because the user's response takes longer than a few seconds.
I thought a solution would be to cancel the logoff / shutdown / restart, and resume it when the user answered to the file save dialog.
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 ?
}
The problem is that ReasonSessionEnding does not tell me if Windows was shutting down or restarting (it does not differentiate between the two).
So, what should my program do on the session ending event ?
Should it even do anything, or doing nothing on this event is the standard ?
The user is asked to save his changes in my main form's OnClosing method, so he does not lose data, but I think that the "application does not respond" window does not suggest a normal workflow.
Canceling the shutdown is not desired I guess, because some of the other programs have been shut down already.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无论如何,似乎可接受的方法是显示“另存为”对话框。
出于您所说的原因和其他各种原因,取消关闭然后稍后恢复肯定不是一个选择。
由于简单地丢弃数据是不可接受的,因此确实没有其他选择。
好吧,除了将数据保存到临时文件,然后在下次运行程序时自动恢复它们。 就像崩溃后的 MS Word 一样。 事实上,我越考虑,听起来就越好。
编辑:还有另一种途径,即连续保存,例如。 MS OneNote 可以。 之前让我印象深刻的是,如果您在应用程序中实现了像样的多级撤消,整个手动保存业务实际上有点过时了——从磁盘操作昂贵且容易出错的时代来看,这是不合时宜的,现在大多是旧习惯。
但我离题了。 无论如何,它可能不适用于您的应用程序,因为我想它需要从一开始就实现。
What seems to be the accepted way is to display the save as dialog regardless.
Cancelling the shutdown, then resuming it later is most certainly not an option, for the reason you state and various others.
Since simply discarding the data is unacceptable, there really is no other options.
Well, except to save the data to a temporary file, then automatically restoring them the next time the program is run. Rather like MS Word after it has crashed. Actually, the more I consider it, the better it sounds.
Edit: There's yet another avenue, namely to save continously, the way eg. MS OneNote does. What has struck me before is that, provided you implement decent multilevel undo in your application, the whole manual saving business is actually somewhat dated - an anachronism from the days when disk operations were expensive and error-prone, nowadays mostly old habit.
But I'm digressing. Anyway, it's probably not applicable to your application, since I imagine it needs to be implemented from the beginning.