15

At the office, when I leave for the night I very rarely log off or reboot. I simply lock my workstation and go home, leaving all my development tools exactly how I left them.

If Windows-Update rolls through and reboots my machine in the middle of the night I'm only slightly peeved because when I log back in the next morning, any MS Office application, or Visual Studio instance I had running will have already automatically restarted, opening whatever file(s)/projects/solutions I may have been working on.

My question is: How can I make my Windows Forms applications (C#) do this? Is there some way for my application to "register" that it wants to be restarted if the system automatically reboots?

Yoopergeek
  • 5,592
  • 3
  • 24
  • 29
  • I would like to know that for WPF applications as well. One would expect this would be independent of GUI framework. – JCCyC Jul 02 '09 at 21:05
  • have you tried putting the executable in the startup? – northpole Jul 02 '09 at 21:06
  • @birdlips: That is not the desired behavior. I do not want my application *always* starting when Windows starts. Only if it was running when Windows Update shutdown Windows. – Yoopergeek Jul 02 '09 at 21:09
  • I would assume Office and VS are restarted with no need to put them in startup. (Putting Office in Startup? Ewwwww!) – JCCyC Jul 02 '09 at 21:09
  • 1
    In think that both Office and Visual Studio use the restart and recovery API (although maybe the native one, not the managed one). I just heard about it on a .NET Rocks episode. – Jacob Adams Jul 02 '09 at 21:12

4 Answers4

9

I think the RegisterApplicationRestart Win32 API function might be what you're after, it's part of the Restart Manager API.

Rob
  • 45,296
  • 24
  • 122
  • 150
4

If you have Windows Vista or Windows 7, you can use the Managed Restart and Recovery API. The links on that page also point to some useful blog entries

http://channel9.msdn.com/posts/DanielMoth/Windows-Vista-Restart-amp-Recovery-APIs-from-managed-code/

Jacob Adams
  • 3,944
  • 3
  • 26
  • 42
  • 1
    A small point (having just re-read Daniel Moth's blog entries, I remember reading them when originally published now!) - there's not actually a Managed API, what DM demonstrates is how to call the WIn32 API from managed code =) – Rob Jul 02 '09 at 21:27
  • Good video link. But it doesn't answer the question: I want my application to restart upon Windows restart when it's forced to close because Windows Update is forcing a restart of Windows, not when my application fails in a firey ball of crashy flames. – Yoopergeek Jul 02 '09 at 21:34
  • I think it does answer your question. I think the function will be called in either case. Here is the link to the .NET Rocks transcripts that seems to indicate it. Search for "restart" around page 7 http://perseus.franklins.net/dotnetrocks_0443_kate_gregory.pdf – Jacob Adams Jul 02 '09 at 22:36
  • Hmmm, yes, they do start talking about it on page 7 of the transcript, and what Kate Gregory is saying makes it sound like it can do exactly what I'm looking for. It wasn't until just now that I realized that this is going to be a major pain to test. ;) I'm going to have fire up some non-updated VM I think... – Yoopergeek Jul 03 '09 at 02:17
2

A simple way is to add an entry to the following registry key :

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce

Just create a value containing the path of your app (optionally including command line arguments). The app will be run at the next startup, then the value will be deleted.

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
0

Step 1: Figure out a way to differentiate a windows-triggered restart from a standard one. One solution would be to try preprocessing messages. They're probably different for a windows-triggered restart...or at least they are in Vista in some cases :/

Step 2: If you detect it's a windows-triggered restart, add a scheduled, one-time task.

Brian
  • 25,523
  • 18
  • 82
  • 173