3 using System
.Diagnostics
;
4 using Microsoft
.SDK
.Samples
.VistaBridge
.Library
.AppRecoveryRestart
;
6 namespace Server2008
.DeveloperStory
.ARRExamples
10 static void Main(string[] args
)
12 RegisterForRecovery();
15 // SetupTimerNotifyForRestart sets a timer to
16 // beep when 60 seconds have elapsed, indicating that
17 // WER will restart the program after a crash.
18 // WER will not restart applications that crash
19 // within 60 seconds of startup.
20 SetupTimerNotifyForRestart();
22 // If we started with /restart command line argument
23 // then we were automatically restarted and should
24 // try to resume the previous session.
25 if (args
.Length
> 0 && args
[0] == "/restart")
27 RecoverLastSession(args
[0]);
29 // Main loop of activities that the user can select.
33 string s
= GetUserChoiceFromMenu();
36 case "1": DisplayRecoverySettings();
38 case "2": DisplayRestartSettings();
42 case "q": done
= true;
46 while (done
== false);
49 private static void SetupTimerNotifyForRestart()
51 // Beep when 60 seconds has elapsed.
52 Timer notify
= new Timer(60000);
53 notify
.Elapsed
+= new ElapsedEventHandler(NotifyUser
);
54 notify
.AutoReset
= false; // Only beep once.
55 notify
.Enabled
= true;
58 private static void NotifyUser(object source
, ElapsedEventArgs e
)
63 private static string GetUserChoiceFromMenu()
65 string banner
= "-----------------------------------";
67 // Display the main menu and get the user's input.
69 Console
.WriteLine(banner
);
70 Console
.WriteLine("ARR Demo Main Menu");
71 Console
.WriteLine(banner
);
72 Console
.WriteLine("1 - Show Recovery settings");
73 Console
.WriteLine("2 - Show Restart settings");
74 Console
.WriteLine("3 - Crash this application");
75 Console
.WriteLine("q - Exit this program");
77 Console
.Write("Enter your option: ");
79 string s
= Console
.ReadLine().ToLower();
80 if (s
!= "1" && s
!= "2" && s
!= "3" && s
!= "q")
82 Console
.WriteLine("Invalid Option: {0}", s
);
83 return GetUserChoiceFromMenu();
88 private static void Crash()
90 Environment
.FailFast("ARR Demo intentional crash.");
92 private static void RegisterForRestart()
94 // Register for automatic restart if the
95 // application was terminated for any reason
96 // other than a system reboot or a system update.
97 ArrManager
.RegisterForApplicationRestart(
100 RestartRestrictions
.NotOnReboot
| RestartRestrictions
.NotOnPatch
));
103 private static void RegisterForRecovery()
105 RecoverySettings settings
= new RecoverySettings(
106 new RecoveryCallback(RecoveryProcedure
),
107 new RecoveryData(Environment
.UserName
),
110 ArrManager
.RegisterForApplicationRecovery(
114 // This method is invoked by WER.
115 private static int RecoveryProcedure(RecoveryData parameter
)
117 Console
.WriteLine("Recovery in progress for {0}",
118 parameter
.CurrentUser
);
120 // Do recovery work here.
121 for (int i
= 0; i
< 4; i
++)
123 // Signal to WER that the recovery
124 // is still in progress.
126 // Simulate long running recovery.
127 System
.Threading
.Thread
.Sleep(3000);
129 // Indicate that recovery work is done.
130 Console
.WriteLine("Application shutting down...");
131 ArrManager
.ApplicationRecoveryFinished(true);
135 // This method is called periodically to ensure
136 // that WER knows that recovery is still in progress.
137 private static void PingSystem()
139 // Find out if the user canceled recovery.
141 ArrManager
.ApplicationRecoveryInProgress();
145 Console
.WriteLine("Recovery has been canceled by user.");
148 Console
.WriteLine(" / ");
151 // This method gets called by main when the
152 // commandline arguments indicate that this
153 // application was automatically restarted
155 private static void RecoverLastSession(string command
)
157 Console
.WriteLine("Recovery in progress {0}", command
);
158 // Perform application state restoration
162 private static void DisplayRecoverySettings()
164 RecoverySettings settings
=
165 ArrManager
.ApplicationRecoverySettings(
166 Process
.GetCurrentProcess().Handle
);
168 Console
.WriteLine(settings
.ToString());
172 private static void DisplayRestartSettings()
174 RestartSettings settings
=
175 ArrManager
.ApplicationRestartSettings(
176 Process
.GetCurrentProcess().Handle
);
178 Console
.WriteLine(settings
.ToString());