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

C

Claire

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;

}
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
C

Claire

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
 
C

Claire

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top