473,503 Members | 1,657 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Windows won't shut down if my application's hidden with notify icon showing in tray

Windows refuses to close down if my applications main form is hidden and
there's a notify icon in the system tray. If I restore the form, then
shutting down Windows succeeds.
If I comment out the code in "frmOptions_Closing" then run and hide the
application, Windows shuts down normally.

I think there was a way to find out what was causing an application to close
down - api call? Is there anything in .net that I can query

My code is as follows

private void frmOptions_Resize(object sender, System.EventArgs e)

{

if (WindowState == FormWindowState.Minimized)

{

ShowInTaskbar = false;

Hide();

}

}

private void mnuOptions_Click(object sender, System.EventArgs e)

{

Show();

WindowState = FormWindowState.Normal;

ShowInTaskbar = true;

BringToFront();

}

private bool DOCLOSE = false;

private void mnuClose_Click(object sender, System.EventArgs e)

{

DOCLOSE = true;

Close();

}

private void frmOptions_Closing(object sender,
System.ComponentModel.CancelEventArgs e)

{


if (DOCLOSE == false)

{

e.Cancel = true;

WindowState = FormWindowState.Minimized;

}


}

private void cmdMinimize_Click(object sender, System.EventArgs e)

{

Close();

}

private void frmOptions_SizeChanged(object sender, System.EventArgs e)

{

if (WindowState == FormWindowState.Normal)

Settings.OptionBounds = Bounds;

}
Nov 16 '05 #1
3 2914
Claire,

When shutting down, your form is queried to determine if it should close
or not. Because you return true for the cancel operation, it cancels the
shutdown.

What you have to do is intercept the windows message for the closing of
the form (which has the reason in it), and if it is a shutdown notification,
allow the shutdown to occur. Basically, you want to look for the
WM_QUERYENDSESSION message (0x11). When you get it, you should set a flag
internally indicating that the form is closing, and then, in your handler,
do not close if the flag is set.

In .NET 2.0, this issue is resolved, with the addition of properties on
the event args when a form is closing indicating the reason for the closing.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Claire" <as*******@ntlworld.com> wrote in message
news:uL**************@TK2MSFTNGP15.phx.gbl...
Windows refuses to close down if my applications main form is hidden and
there's a notify icon in the system tray. If I restore the form, then
shutting down Windows succeeds.
If I comment out the code in "frmOptions_Closing" then run and hide the
application, Windows shuts down normally.

I think there was a way to find out what was causing an application to
close down - api call? Is there anything in .net that I can query

My code is as follows

private void frmOptions_Resize(object sender, System.EventArgs e)

{

if (WindowState == FormWindowState.Minimized)

{

ShowInTaskbar = false;

Hide();

}

}

private void mnuOptions_Click(object sender, System.EventArgs e)

{

Show();

WindowState = FormWindowState.Normal;

ShowInTaskbar = true;

BringToFront();

}

private bool DOCLOSE = false;

private void mnuClose_Click(object sender, System.EventArgs e)

{

DOCLOSE = true;

Close();

}

private void frmOptions_Closing(object sender,
System.ComponentModel.CancelEventArgs e)

{


if (DOCLOSE == false)

{

e.Cancel = true;

WindowState = FormWindowState.Minimized;

}


}

private void cmdMinimize_Click(object sender, System.EventArgs e)

{

Close();

}

private void frmOptions_SizeChanged(object sender, System.EventArgs e)

{

if (WindowState == FormWindowState.Normal)

Settings.OptionBounds = Bounds;

}

Nov 16 '05 #2
That's exactly what I needed and it's working great, thanks vm Nicholas.
I had forgotten the message name and was very surprised when I saw the lack
of information in the event args object.
Quite important omission on microsoft's part, glad they've fixed it in the
next edition.

Claire
Nov 16 '05 #3
btw, you answered a query back in october giving me similar response but for
some reason it didn't work for me then. Ive changed my code since as I was
setting and testing the DialogResult property originally, Ive now used a
simple flag instead so that may have been screwing things up.
Thanks again.

Claire
Nov 16 '05 #4

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

Similar topics

7
10279
by: Lalit | last post by:
Hi Friends, I have developed a Windows service. Now i need icon for this service in systray and context menu fo this icon. Can i do this? With regards, Lalit
11
8923
by: Andrew Mueller | last post by:
Hello, Is there any way to make a form part of a windows service application and launch it upon double-click of a system tray icon? I already have the NotifyIcon working and have added an...
4
8794
by: David Meier | last post by:
I have an application running in the background, visible only by a notify icon in the system tray. I learned from other posts to catch the WM_QUERYENDSESSION and WM_ENDSESSION messages. Now, when I...
4
5833
by: Claire | last post by:
Running XP pro, SP2. Visual studio .NET 2003. App written in C# I have written an application that hides itself when run and shows a notification icon in the system tray. If the main form is...
8
2257
by: Jacek Jurkowski | last post by:
1) How to prevent user to run secound copy of application on the same mahine? I would like to show dialog "Already run" instead of launching a secound insrance. 2) Why, if minimized into a notify...
4
3396
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...
3
1676
by: =?Utf-8?B?Y2h1Y2sgcnVkb2xwaA==?= | last post by:
Does anyone know an approach to knowing when your application with a notify icon is being shut down by windows? I have an application with a notify icon and a back ground thread. The back ground...
5
1680
by: cameljs18 | last post by:
Okay well maybe my heading isn't exactle right but what i want to do is create a windows application that loads when the machine starts up but instead of having it on the screen i just want it to...
0
7198
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
7271
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,...
0
7449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4998
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4666
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...
0
3160
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...
0
3149
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1498
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 ...
0
373
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.