473,385 Members | 1,813 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,385 software developers and data experts.

How to know when system is shutting down?

mdb
I have an app that I want to keep open (and hidden with an icon in the
taskbar) when the user presses the 'X' close window button. So I capture
the Closing event on the form and check a bool variable, which if set, sets
e.Cancel on the ClosingEventArgs and sets the Form.Visible=false and the
Form.ShowInTaskbar=false. I have a menu item in the window that allows the
user to actually exit the program by unsetting the bool and then exiting.

The problem is that this apparently prevents the computer from shutting
down, because the e.Cancel is getting set during a shutdown operation. How
can I determine when it is the system requesting closure of the window
instead of the user?

-mdb
Nov 17 '05 #1
5 6014
Hi mdb,

In the Closing event, read the StackTrace to determine who called the method.
The 7th last StackFrame is the method triggering the event.

protected override void OnClosing(CancelEventArgs e)
{
StackTrace trace = new StackTrace();
StackFrame frame = trace.GetFrame(7);

if(frame.GetMethod().Name != "DispatchMessageW")
e.Cancel = true;
}

DispatchMessageW is the same as Task Manager-closing of your application, not sure if it would be the same for system shutdown.

"RunDialog"/"SendMessage" = closed by code. ie this.Close();
"DefWndProc" = User close, X or Alt-4
--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #2
Look at System.Environment.HasShutdownStarted
--
--
Truth,
James Curran
[erstwhile VC++ MVP]

Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

"mdb" <m_b_r_a_y@c_t_i_u_s_a__d0t__com> wrote in message
news:Xn****************************@207.46.248.16. ..
I have an app that I want to keep open (and hidden with an icon in the
taskbar) when the user presses the 'X' close window button. So I capture
the Closing event on the form and check a bool variable, which if set, sets e.Cancel on the ClosingEventArgs and sets the Form.Visible=false and the
Form.ShowInTaskbar=false. I have a menu item in the window that allows the user to actually exit the program by unsetting the bool and then exiting.

The problem is that this apparently prevents the computer from shutting
down, because the e.Cancel is getting set during a shutdown operation. How can I determine when it is the system requesting closure of the window
instead of the user?

-mdb

Nov 17 '05 #3

"mdb" <m_b_r_a_y@c_t_i_u_s_a__d0t__com> wrote in message
news:Xn****************************@207.46.248.16. ..
I have an app that I want to keep open (and hidden with an icon in the
taskbar) when the user presses the 'X' close window button. So I capture
the Closing event on the form and check a bool variable, which if set,
sets
e.Cancel on the ClosingEventArgs and sets the Form.Visible=false and the
Form.ShowInTaskbar=false. I have a menu item in the window that allows
the
user to actually exit the program by unsetting the bool and then exiting.

The problem is that this apparently prevents the computer from shutting
down, because the e.Cancel is getting set during a shutdown operation.
How
can I determine when it is the system requesting closure of the window
instead of the user?

-mdb


Handle the WM_QUERYENDSESSION mesage in your overriden WndProc for your
form.
Something like this should do..

private static bool shutdownRequested = false;
protected override void WndProc(ref Forms.Message msg)
{
const int WM_QUERYENDSESSION = 0x11;
if (msg.Msg==WM_QUERYENDSESSION)
{
// set this flag to true if the System is about to shutdown
// Check this flag in your Closing handler.
shutdownRequested = true;
}

// If this is WM_QUERYENDSESSION, the closing event should be fired
// in the base WndProc
base.WndProc(m);
}
Willy.

Nov 17 '05 #4

"James Curran" <ja*********@mvps.org> wrote in message
news:uX**************@TK2MSFTNGP14.phx.gbl...
Look at System.Environment.HasShutdownStarted
--
--


James,

This flag is set when the CLR is about to shut down not as a direct result
of a system shutdown, this flag is set way after the Closing events are
fired and as such cannot be used in this scenario.

Willy.

Nov 17 '05 #5

"cody" <de********@gmx.de> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Why not simply use the SystemEvents.SessionEnding event?

Handle the WM_QUERYENDSESSION mesage in your overriden WndProc for your
form.
Something like this should do..

private static bool shutdownRequested = false;
protected override void WndProc(ref Forms.Message msg)
{
const int WM_QUERYENDSESSION = 0x11;
if (msg.Msg==WM_QUERYENDSESSION)
{
// set this flag to true if the System is about to shutdown
// Check this flag in your Closing handler.
shutdownRequested = true;
}

// If this is WM_QUERYENDSESSION, the closing event should be fired
// in the base WndProc
base.WndProc(m);
}



Because OP needs to do some processing before the Closing event gets fired,
see Remarks in SessionEnding to how this should be done.

Willy.

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: Nancy Drew | last post by:
hi all i'm trying to keep users from being able to login to a site twice at the same time. everytime a user does a login, i stick their userID into an application scoped array. if they try to...
3
by: kuldeep | last post by:
Hi all, I have a application developed in C# .net. The problem I face is I am unable to shutdown my machine when the exe is running. Windows is unable to close this exe an shut down. Can anyone...
0
by: mikelostcause | last post by:
Is there anyway to hold the base.WndProc(ref m) until after the Logout() finishes loading a webpage?? I'm working on shutting down an app that runs in the system tray, I have no problems...
0
by: mikelostcause | last post by:
Is there anyway to hold the base.WndProc(ref m) until after the Logout() function finishes loading a webpage?? I'm working on shutting down an app that runs in the system tray, I have no...
1
by: mikelostcause | last post by:
Is there anyway to hold the base.WndProc(ref m) until after the Logout() function finishes loading a webpage?? I'm working on shutting down an app that runs in the system tray, I have no...
2
by: Mike Stephens | last post by:
I have an application to minimizes when X is clicked. If the user wants to close the application they click the Exit Application button.This works fine and does exactly what I need. I have since...
4
by: Markus Stoeger | last post by:
Hi, I have a problem with Application.Run() when Windows is shutting down. Please have a look at the copy&paste example program below. The application has no forms. It has only got a notify...
0
by: bar10dr | last post by:
What I want to do is get song info from iTunes if the application is running, using the ituneslib API (from Apple). One of the problem is that to check if iTunes is playing any songs I need to...
2
by: Jack | last post by:
Sorry for the double post (also in the IIS group). We've got an ASP.Net 2.0 app running on IIS6. We kept losing sessions, and enabled health monitoring to see what was happening. This morning...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
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.