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.

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

{<br />

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

"RestartDemo begin OnStartup");<br />

}<br />

}<br />

foreach (var arg in e.Args)<br />

{<br />

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

String.Format("argument {0}", arg));<br />

if (arg.StartsWith("/restart:",<br />

StringComparison.InvariantCultureIgnoreCase))<br />

{<br />

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

"/restart: argument found");<br />

RestartPath = arg.Substring(9);<br />

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

String.Format("RestartPath: {0}",<br />

RestartPath));<br />

}<br />

}<br />

}<br />

public string RestartPath { get; private set; }<br />

code snippet EditorDemo/App.xaml.cs<br />

The main window (see Figure A-1) just contains<br />

a TextBox for editing the text in the middle<br />

area, a TextBlock control in the bottom area for<br />

status messages, <strong>and</strong> a Menu control.<br />

The constructor of the MainWindow class checks<br />

to see if the application was started from a<br />

recovery, by checking the RestartPath property<br />

from the App class. If the application was started<br />

because of a recovery, the filename assigned to<br />

the RestartPath property is used to load this file<br />

<strong>and</strong> assign it to the DataContext of the TextBox.<br />

If the application was started normally, a new<br />

filename for recovery is created.<br />

Inside the constructor, a DispatcherTimer is also created that fires after 60 seconds. This is used to inform<br />

the user that the application can only recover from a crash after running for at least 60 seconds.<br />

The most important calls in this scenario are the helper methods RegisterForRestart() <strong>and</strong><br />

RegisterForRecovery(), which are called to register with the Restart Manager.<br />

using System;<br />

using System.Diagnostics;<br />

using System.Threading;<br />

using System.Windows;<br />

using System.Windows.Controls;<br />

using System.Windows.Input;<br />

using System.Windows.Threading;<br />

using Microsoft.WindowsAPICodePack.ApplicationServices;<br />

namespace Wrox.ProCSharp.Windows7<br />

{<br />

public partial class MainWindow : Window<br />

{<br />

private DispatcherTimer minuteTimer;<br />

figure a-1<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!