Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Exact C# Code Sought to Capture System Shutdown Event in Windows Application

Posted on 2009-04-15
2
Medium Priority
?
956 Views
Last Modified: 2013-12-17
I've read a bunch of posts and off-site URLs on E-E but don't understand how to structure code in C# to capture a system shutdown event so that my forms based app has time to cleanup things before the inevitable end.

I'm seeking three things:
1. What do I put in Form1_Load() method?
2. What is exact C# signature for Closing/OnBeginShutdown/etc. event?
3. What value do I check inside shutdown event that indicates system shutdown?

I haven't found one page the answers these questions in C#... only VB which doesn't help me.
0
Comment
Question by:ZuZuPetals
2 Comments
 
LVL 15

Accepted Solution

by:
oobayly earned 2000 total points
ID: 24148459
http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.sessionending.aspx

http://msdn.microsoft.com/en-us/library/microsoft.win32.systemevents.sessionending.aspx
// Hook
Microsoft.Win32.SystemEvents.SessionEnding += new Microsoft.Win32.SessionEndingEventHandler(SystemEvents_SessionEnding);
 
// Handle
    static void SystemEvents_SessionEnding(object sender, Microsoft.Win32.SessionEndingEventArgs e) {
      switch (e.Reason) {
        case Microsoft.Win32.SessionEndReasons.Logoff:
          break;
 
        case Microsoft.Win32.SessionEndReasons.SystemShutdown:
          break;
 
      }

Open in new window

0
 
LVL 2

Author Closing Comment

by:ZuZuPetals
ID: 31570463
Wonderful answer!  Thanks!
0

Featured Post

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question