By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,919 Members | 1,587 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,919 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.