Jump to content

Computer shutdown event?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
X_Programmer

X_Programmer

    Learning Programmer

  • Members
  • PipPipPip
  • 89 posts
Hey guys,
I've been wondering if there is a computer shutdown event in C#.

I have an application that runs in the background and once the closing event is fired it saves some data to a text file. Problem is, the closing event is not triggered when you shutdown the computer, and I need the application to save the data on shutdown.

I've tried searching the internet, but I can't find anything that works. If there isn't a straight forward event what's the easiest way to detect when the computer is shutting down?


Thanks,
X_Pro

Edited by X_Programmer, 22 April 2010 - 06:25 AM.


#2
semprance

semprance

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
I made a little program a while back to do timed shutdowns (so I can watch crap on my computer and not have to get out of bed to turn it off). I struggled working out how to do it at the time but its actually quite easy. Here's the snippet you'll find most useful:


    System.Diagnostics.Process.Start("c:/windows/system32/shutdown.exe", "-s -f -t 0");


In terms of parameters, '-s' implies shutdown (instead of restart etc.), '-f' is force shutdown, and '-t 0' is shutdown immediately (ie. no timer). For more parameter info, open up DOS and type 'help shutdown'.

If you want full source code, message back and I'll post it somewhere.

#3
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
Note :
The [URL="http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing.aspx"]FormClosing[/URL]  event occurs just before a form is closed, either by the user, through  the user interface (UI), 
or programmatically, through calls to methods  such as [URL="http://msdn.microsoft.com/en-us/library/system.windows.forms.form.close.aspx"]Close[/URL]  in the [URL="http://msdn.microsoft.com/en-us/library/system.windows.forms.form.aspx"]Form[/URL]  class, or [URL="http://msdn.microsoft.com/en-us/library/system.windows.forms.application.exit.aspx"]Exit[/URL]  in the [URL="http://msdn.microsoft.com/en-us/library/system.windows.forms.application.aspx"]Application[/URL]  class. 
If a form has any  child or owned forms, a [URL="http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing.aspx"]FormClosing[/URL]  event is also raised for each one. If any one of the forms cancels
 the  event, none of the forms are closed. Therefore the corresponding [URL="http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosed.aspx"]FormClosed[/URL]  events are not sent to any of the forms.
The FormClosingEventArgs  class provides data for this event. Two important members are the [URL="http://msdn.microsoft.com/en-us/library/system.componentmodel.canceleventargs.cancel.aspx"]Cancel[/URL]  and
 [URL="http://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.closereason.aspx"]CloseReason[/URL]  properties.
 The event can be canceled by setting the [URL="http://msdn.microsoft.com/en-us/library/system.componentmodel.canceleventargs.cancel.aspx"]Cancel[/URL]  property to true. The [URL="http://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.closereason.aspx"]CloseReason[/URL]  property provides a reason why the form is being closed.
So this is just a piece of cake , when the form is closing check for CloseReason argument. Then do ,your Job that application should take care off.

have a look at the MSdn documentation
http://msdn.microsoft.com/en-us/library/system.windows.forms.formclosingeventargs.aspx
Feed Me More +Reps

#4
semprance

semprance

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
Lol sorry, I completely misread the first post, thought you were wondering how to start a shutdown. gokuajmes has got it covered :-)

#5
X_Programmer

X_Programmer

    Learning Programmer

  • Members
  • PipPipPip
  • 89 posts
Awesome, thanks guys(I learned to find the closing reason AND as an unexpected bonus learned how to shut down a computer XD).

#6
gokuajmes

gokuajmes

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 518 posts
Glad we could help You , Our pleasure ping back with an awesome application you made with this gained knowledge :D