Solved

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

Posted on 2009-04-15
2
884 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

632 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