added samples
[windows-sources.git] / sdk / samples / CrossTechnologySamples / VistaBridge / applicationrecoveryrestartdemoapp / program.cs
blob3b032e15f85fdfca365942603101661d278d8007
1 using System;
2 using System.Timers;
3 using System.Diagnostics;
4 using Microsoft.SDK.Samples.VistaBridge.Library.AppRecoveryRestart;
6 namespace Server2008.DeveloperStory.ARRExamples
8 class ARRDemoApp
10 static void Main(string[] args)
12 RegisterForRecovery();
13 RegisterForRestart();
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.
30 bool done = false;
33 string s = GetUserChoiceFromMenu();
34 switch (s)
36 case "1": DisplayRecoverySettings();
37 break;
38 case "2": DisplayRestartSettings();
39 break;
40 case "3": Crash();
41 break;
42 case "q": done = true;
43 break;
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)
60 Console.Beep();
63 private static string GetUserChoiceFromMenu()
65 string banner = "-----------------------------------";
67 // Display the main menu and get the user's input.
68 Console.WriteLine();
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");
76 Console.WriteLine();
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();
85 return s;
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(
98 new RestartSettings(
99 "/restart",
100 RestartRestrictions.NotOnReboot | RestartRestrictions.NotOnPatch));
103 private static void RegisterForRecovery()
105 RecoverySettings settings = new RecoverySettings(
106 new RecoveryCallback(RecoveryProcedure),
107 new RecoveryData(Environment.UserName),
108 4000);
110 ArrManager.RegisterForApplicationRecovery(
111 settings);
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.
125 PingSystem();
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);
132 return 0;
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.
140 bool isCanceled =
141 ArrManager.ApplicationRecoveryInProgress();
143 if (isCanceled)
145 Console.WriteLine("Recovery has been canceled by user.");
146 Environment.Exit(2);
148 Console.WriteLine(" / ");
151 // This method gets called by main when the
152 // commandline arguments indicate that this
153 // application was automatically restarted
154 // by WER.
155 private static void RecoverLastSession(string command)
157 Console.WriteLine("Recovery in progress {0}", command);
158 // Perform application state restoration
159 // actions here.
162 private static void DisplayRecoverySettings()
164 RecoverySettings settings =
165 ArrManager.ApplicationRecoverySettings(
166 Process.GetCurrentProcess().Handle);
168 Console.WriteLine(settings.ToString());
169 Console.WriteLine();
172 private static void DisplayRestartSettings()
174 RestartSettings settings =
175 ArrManager.ApplicationRestartSettings(
176 Process.GetCurrentProcess().Handle);
178 Console.WriteLine(settings.ToString());
179 Console.WriteLine();