473,805 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 ClosingEventArg s and sets the Form.Visible=fa lse and the
Form.ShowInTask bar=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 6034
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(Cance lEventArgs e)
{
StackTrace trace = new StackTrace();
StackFrame frame = trace.GetFrame( 7);

if(frame.GetMet hod().Name != "DispatchMessag eW")
e.Cancel = true;
}

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

"RunDialog"/"SendMessag e" = 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.Environm ent.HasShutdown Started
--
--
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__c om> wrote in message
news:Xn******** *************** *****@207.46.24 8.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 ClosingEventArg s and sets the Form.Visible=fa lse and the
Form.ShowInTask bar=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__c om> wrote in message
news:Xn******** *************** *****@207.46.24 8.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 ClosingEventArg s and sets the Form.Visible=fa lse and the
Form.ShowInTask bar=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_QUERYENDSESS ION mesage in your overriden WndProc for your
form.
Something like this should do..

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

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

Nov 17 '05 #4

"James Curran" <ja*********@mv ps.org> wrote in message
news:uX******** ******@TK2MSFTN GP14.phx.gbl...
Look at System.Environm ent.HasShutdown Started
--
--


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******** ********@TK2MSF TNGP10.phx.gbl. ..
Why not simply use the SystemEvents.Se ssionEnding event?

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

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

// If this is WM_QUERYENDSESS ION, 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
2465
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 login again, i bounce them to an error page. i use the session_onEnd sub within global.asa to remove their userID from the array at the end of their session, and this seems to work fine. however, if i just shut down the browser, the sub_onEnd...
3
3482
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 help. Regards, Kuldeep Pawar
0
1142
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 shutting down, but I have problems saving data first. if the base.WndProc(ref m) is placed at the top, it closes, but the system does not continue to shutdown and it does not save the data via the Logout() funtion. If the base.WndProc(ref m) is placed...
0
1083
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 problems shutting down, but I have problems saving data first. if the base.WndProc(ref m) is placed at the top, it closes, but the system does not continue to shutdown and it does not save the data via the Logout() funtion. If the base.WndProc(ref m)...
1
2126
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 problems shutting down, but I have problems saving data first. if the base.WndProc(ref m) is placed at the top, it closes, but the system does not continue to shutdown and it does not save the data via the Logout() funtion. If the base.WndProc(ref m)...
2
19939
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 discovered it is holding Windows open when you shutdown. If the application is running and the user shutsdown Window it holds up everything. If the application is not running and the user shutsdown Windows is shutsdown in a timely manner. How...
4
3411
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 icon in the system tray and it uses Application.Run() to keep the message loop running. When the user clicks the icon, the application should shut down and exit. So far that works fine.
0
2354
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 make the COM object right? But when I make it, it starts up the iTunes.exe application if iTunes is not already running. To counter that I made checkiTunesAlive, wich goes trough the running processes in windows and returns true if iTunes.exe is...
2
13677
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 the got the following in the logs. Any idea what would cause it? Event code: 1002 Event message: Application is shutting down. Reason: Hosting environment is shutting down. Event time: 7/17/2007 10:28:37 AM
0
9716
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10359
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10364
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9182
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6875
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5541
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4317
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3843
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.