What is the proper way to handle output / shutdown / restart when the application has unsaved data?

In WPF App.Current.SessionEnding should return after a few seconds, otherwise the "application does not respond" window will appear. Therefore, the user cannot request this event handler to save his data, since the user's response takes more than a few seconds.

I thought that the solution would be to cancel the logout / shutdown / restart and resume it when the user answered 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 that Windows shuts down or restarts (it does not distinguish between them).

So what should my program do at the session end event? Should he do anything at all or do nothing at this event, is this the standard?

The user asks to save his changes in my main OnClosing form, so he does not lose data, but I think that the "application does not respond" window does not imply a normal workflow.

Cancel shutdown is undesirable. I assume that some other programs are already closed.

+4
source share
1 answer

What seems acceptable is to display the save dialog as independent.

Canceling the shutdown and then resuming it later is certainly not an option, due to your condition and others.

Since simply discarding data is unacceptable, there are no other options.

Well, except for saving data to a temporary file, then automatically restoring it the next time the program starts. Rather like MS Word after it crashed. In fact, the more I think this is, the better.

Edit: There is another way, namely: save, for example, the path. MS OneNote does. What struck me before is that provided that you are performing a decent multi-level cancellation in your application, all manual business savings are actually somewhat dated - an anachronism since when disk operations were expensive and error prone, currently mostly old habits.

But I'm distracted. In any case, it is probably not applicable to your application, since I assume that it should be implemented from the very beginning.

+5
source

All Articles