Solved

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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now