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.

application recovery ❘ 1399<br />

<strong>and</strong> Content properties, <strong>and</strong> Load() <strong>and</strong> Save() methods. The application makes use of tracing to write<br />

verbose messages to a trace listener so that you can retrace the events from the application.<br />

Tracing is discussed in Chapter 19, “Instrumentation.”<br />

using System;<br />

using System.Diagnostics;<br />

using System.IO;<br />

namespace Wrox.ProCSharp.Windows7<br />

{<br />

public class CurrentFile<br />

{<br />

public bool IsDirty { get; set; }<br />

public string Content { get; set; }<br />

public void Load stringfileName) (<br />

{<br />

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

String.Format("begin Load {0}", fileName));<br />

Content = File.ReadAllText(fileName);<br />

IsDirty = false;<br />

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

String.Format("end Load {0}", fileName));<br />

}<br />

}<br />

}<br />

public void Save stringfileName) (<br />

{<br />

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

String.Format("begin Save {0}", fileName));<br />

File.WriteAllText(fileName, Content);<br />

IsDirty = false;<br />

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

String.Format("end Save {0}", fileName));<br />

}<br />

code snippet EditorDemo/CurrentFile.cs<br />

If the application is restarted because of a crash, the /restart:[filename] comm<strong>and</strong> - line argument is<br />

passed to the executable. To retrieve comm<strong>and</strong> - line information, the OnStartup() h<strong>and</strong>ler method is assigned<br />

to the Startup event of the App class. With the second argument, which is of type StartupEventArgs , the<br />

comm<strong>and</strong> - line arguments can be retrieved. With the /restart: argument, the following filename is assigned<br />

to the RestartPath property. The App class also contains the TraceSource object, which is used for tracing.<br />

using System;<br />

using System.Diagnostics;<br />

using System.Windows;<br />

namespace Wrox.ProCSharp.Windows7<br />

{<br />

public partial class App : Application<br />

{<br />

public static TraceSource Source = new TraceSource("RestartDemo");<br />

private void OnStartup(object sender, StartupEventArgs e)<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!