15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

application recovery ❘ 1403<br />

}<br />

textStatus.Text += String.Format(“{0:T}: Registered for recovery\n”,<br />

DateTime.Now);<br />

App.Source.TraceEvent(TraceEventType.Verbose, 0,<br />

“Registered for recovery”);<br />

DoRecovery() is the callback method that is defined by the RegisterForApplicationRecovery() method<br />

call <strong>and</strong> is invoked by WER. Within the h<strong>and</strong>ler, the Recovery Manager is informed that recovery is in<br />

progress by calling the method ApplicationRecoveryInProgress(). This method returns true if the user<br />

canceled the recovery. You can verify user cancellation during the recovery to stop the recovery in case it<br />

was canceled by the user. Here, the content of the TextBox is written to the temporary file that is used when<br />

restarting the application. At the end of the recovery process, the Restart Manager is informed of its success<br />

by invoking ApplicationRecoveryFinished().<br />

private int DoRecovery(object state)<br />

{<br />

App.Source.TraceEvent(TraceEventType.Verbose, 0, "begin Recovery");<br />

this.tempPath = (string)state;<br />

bool canceled = ApplicationRestartRecoveryManager.<br />

ApplicationRecoveryInProgress();<br />

if (canceled)<br />

{<br />

textStatus.Text += String.Format(<br />

"{0:T}: Recovery canceled, shutting down\n", DateTime.Now);<br />

App.Source.TraceEvent(TraceEventType.Verbose, 0,<br />

"end Recovery with cancel");<br />

ApplicationRestartRecoveryManager.ApplicationRecoveryFinished(<br />

false);<br />

return 0;<br />

}<br />

SaveFile(tempPath);<br />

}<br />

App.Source.TraceEvent(TraceEventType.Verbose, 0, "end Recovery");<br />

ApplicationRestartRecoveryManager.ApplicationRecoveryFinished(true);<br />

return 0;<br />

With all this in place, you can run the application, as shown in<br />

Figure A-2. The TextBlock on the bottom gives status information<br />

such as when registering for restart <strong>and</strong> recovery happened, <strong>and</strong> after<br />

60 seconds, information that the application can now crash.<br />

Selecting the menu Action ➪ Crash invokes the h<strong>and</strong>ler method<br />

OnCrash(), which does a fast fail. The method Environment.<br />

FailFast() can be used to terminate a process in a fast way.<br />

This method writes an application event log <strong>and</strong> creates a<br />

dump of the application before the process is terminated. Only<br />

CriticalFinalizerObject objects are executed. Try-finally<br />

blocks <strong>and</strong> finalizers are not executed.<br />

private void OnCrash(object sender, ExecutedRoutedEventArgs e)<br />

{<br />

Environment.FailFast("RestartDemo stopped from Menu comm<strong>and</strong>");<br />

}<br />

After crashing, the application is restarted <strong>and</strong> you can see the<br />

dialog shown in Figure A-3.<br />

figure a-2<br />

figure a-3<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!