15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1402 ❘ aPPendiX Guidelines fOr windOws 7 And windOws server 2008 r2<br />

private void On60Seconds(object sender, EventArgs e)<br />

{<br />

this.textStatus.Text += String.Format("{0:T}: Application can crash " +<br />

"now\n", DateTime.Now);<br />

minuteTimer.Stop();<br />

}<br />

The method RegisterForRestart() invokes the RegisterForApplicationRestart() method<br />

of the ApplicationRestartRecoveryManager class. ApplicationRestartRecoveryManager is<br />

defi ned in the namespace Microsoft.WindowsAPICodePack.ApplicationServices . The method<br />

RegisterForApplicationRestart() wraps the Windows API call RegisterApplicationRestart()<br />

function. With this method, you can defi ne the comm<strong>and</strong> - line arguments that should be used to restart the<br />

application <strong>and</strong> the cases when the application should not be restarted. In this example, it is defi ned that<br />

the application should not be restarted on a reboot or on installation of a patch. You can also specify<br />

that the application should not restart on an application crash <strong>and</strong> on application hang.<br />

private void RegisterForRestart()<br />

{<br />

var settings = new RestartSettings(<br />

String.Format(“/restart:{0}”, tempPath),<br />

RestartRestrictions.NotOnReboot | RestartRestrictions.NotOnPatch);<br />

ApplicationRestartRecoveryManager.RegisterForApplicationRestart(<br />

settings);<br />

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

DateTime.Now);<br />

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

“Registered for restart”);<br />

}<br />

Within the Windows API Code Pack, the Windows API call is defi ned to be invoked by using P/Invoke:<br />

[DllImport("kernel32.dll")]<br />

[PreserveSig]<br />

internal static extern HRESULT RegisterApplicationRestart(<br />

[MarshalAs(UnmanagedType.BStr)] string comm<strong>and</strong>LineArgs,<br />

RestartRestrictions flags);<br />

P/Invoke is discussed in Chapter 26, “Interop.”<br />

With this facility, if the application crashes or hangs, it is restarted. With the Restart Manager, you can<br />

also defi ne a h<strong>and</strong>ler method that is invoked before the application is stopped because of a crash or a<br />

hang. This is done by invoking the RegisterForApplicationRecovery() method of the<br />

ApplicationRestartRecoveryManager class. This method wraps the Windows API function<br />

RegisterApplicationRecoveryCallback() . RegisterForApplicationRecovery() can be confi gured<br />

with an object of type RecoverySettings . With RecoverySettings , you assign an object of type<br />

RecoveryData <strong>and</strong> a recovery ping interval. This ping interval is defi ned in 100 ns units, <strong>and</strong> the callback<br />

method must give progress information to the recovery manager within that time span. By default,<br />

with a value of 0 the time interval is 5 seconds. The maximum value that can be set is 5 minutes. With<br />

RecoveryData you specify the method that is invoked by the Restart Manager to do the recovery, <strong>and</strong> an<br />

argument that is passed to that method:<br />

private void RegisterForRecovery()<br />

{<br />

var settings = new RecoverySettings(new RecoveryData(<br />

DoRecovery, tempPath), 0);<br />

ApplicationRestartRecoveryManager.RegisterForApplicationRecovery(<br />

settings);<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!