473,386 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to detect and abort system shutdown in c# windows application

Subin Ninan
I've tried using Microsoft.Win32.SystemEvents.SessionEnding event, but its not working as expected.
Any idea how to achieve this?
Dec 15 '10 #1

✓ answered by Aimee Bailey

Really bad of me that I find another cheap hack today, but here it is anyway, in winforms, you can handle the FormClosing event of a form, and call a little known console command that aborts shutdown like so...

Expand|Select|Wrap|Line Numbers
  1.     public partial class Form1 : Form
  2.     {
  3.         public Form1()
  4.         {
  5.             InitializeComponent();
  6.         }
  7.  
  8.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  9.         {
  10.             if (e.CloseReason == CloseReason.WindowsShutDown)
  11.             {
  12.                 Process.Start("shutdown", "-a");
  13.             }
  14.         }
  15.     }
  16.  
Best of luck!

Aimee.

3 10910
Aimee Bailey
197 Expert 100+
Really bad of me that I find another cheap hack today, but here it is anyway, in winforms, you can handle the FormClosing event of a form, and call a little known console command that aborts shutdown like so...

Expand|Select|Wrap|Line Numbers
  1.     public partial class Form1 : Form
  2.     {
  3.         public Form1()
  4.         {
  5.             InitializeComponent();
  6.         }
  7.  
  8.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  9.         {
  10.             if (e.CloseReason == CloseReason.WindowsShutDown)
  11.             {
  12.                 Process.Start("shutdown", "-a");
  13.             }
  14.         }
  15.     }
  16.  
Best of luck!

Aimee.
Feb 5 '11 #2
HaLo2FrEeEk
404 256MB
Actually I would recommend catching the WM_QUERYENDSESSION message in WndProc. You can learn more about this message here:

http://msdn.microsoft.com/en-us/library/aa376890.aspx

Wile Aimee's method would work, it's more elegant to use Windows' message system.
Feb 6 '11 #3
That worked!!
Thanks, Aimee & Halo..
Feb 6 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Madhava Patro | last post by:
Hi All, I have a requirement where i have to Shutdown Windows 2000 from a C# Application, my application is running as windows service. When I shutdown Windows it should not reboot again, It...
0
by: Matthew Fitzpatrick | last post by:
My application has a settings class that is serialized to save and deserialized to load. I'm having a problem where i catch the WM_QUERYENDSESSION event via WinProc to save my application's...
3
by: Senthil | last post by:
Hi, I would like to know the code for reboot and shutdown windows xp professional using VB.Net. thanx Senthil
1
by: Josh | last post by:
Hi, I want to detect the keystroke entered by the user in my application. Actually the scenarion is, at first the user is entering some details to the windows form, later on he is idle for few...
2
by: Andy | last post by:
hi all, I've created a windows server that will watch to make sure another process stays running. It looks for the Process and attaches to the Exited event, which works fine. If the program...
4
by: Woo Mun Foong | last post by:
My app sits quitely on the system tray of a file serve, monitoring some users' predefined files. How can I trap the system shutdown message and display a warning. User should be allow to abort...
2
by: Sin Jeong-hun | last post by:
I created a windows form application. It has a Threading.Timer and when the timer ticks it does some work and show a popup window. The problem is that while this program is running if the user...
5
by: Phil Tusa | last post by:
Greetings to all .... I have a need to issue a shutdown and/or Restart Windows XP inside my application. Any help or example code would be appreciated! -- Phil
1
by: icesha | last post by:
hello, im using visual studio 2005, we are creating a system in windows application based. I want t0 create an installer of it where all of the dll of my program will be there. Where I will...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.