Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-04-15
2
Medium Priority
?
926 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 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

730 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