Solved

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

Posted on 2009-04-15
2
845 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 500 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

680 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