views:

583

answers:

5

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?

+4  A: 

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

Rob
Note that the minimum supported client platform is Windows Vista.
Jørn Schou-Rode
This apears to be for C++. There is now a managed restart and recovery api.
Jacob Adams
@Jacob - umm, yup - hence me saying it's a Win32 function, there's nothing to stop the OP making a PInvoke call though =)
Rob
+3  A: 

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
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
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
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 7http://perseus.franklins.net/dotnetrocks_0443_kate_gregory.pdf
Jacob Adams
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
A: 

There's a reallly easy way to do it create a folder called 'start' (or was that StratUp' I always forget) under your menu folder (if you're pre vista the start menu folder is located in documents\settings\user name. Drop a short cut to the application you wish to start and your good to go. Not pretty but it works and can be done from an installer as well real easy

Rune FS
This is not desired behavior. I do not want my application starting *EVERY* time Windows starts. I only want my application to start if it was running when Windows Update forced a Windows shutdown.
Yoopergeek
A: 

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
+1  A: 

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