Closing a notifyicon application

Z

Zamdrist

So I have my notify icon, the form, the context menu all that jazz
working fine.

My notifyicon has a context menu with an Exit option. Works fine. If
the form is current visible and you close is (X)...it hides itself
rather than close. Double click on the notifyicon, and it reappears.

All is good. But...

When you go to shutdown the computer, the computer wont finish
shutting down unless you exit my notifyicon application.

Here is what I'm doing. I have form level variable called bOnExit. In
the Form's Closing event I have:

If Not bOnExit Then
e.Cancel = True
Me.Hide()
End If

In the Notifyicon's double click event I have:

Me.Show()

In the NotifyIcon's Context Menu click event I have:

bOnExit = True
Me.Close()

So obviously, when you shut down windows, bOnExit won't be true, and
therefore the application wont actually exit, and the windows shutdown
will be interrupted.

How is this supposed to be handled. What is the best practice?
Thanks...
 
M

Mattias Sjögren

How is this supposed to be handled. What is the best practice?

Check out the SystemEvents.SessionEnding event. Be sure to read the
docs about its relation to the Closing event and how to handle it
properly.


Mattias
 
Z

Zamdrist

Thank you Mattias, that worked perfectly.

For others struggling with this, I'll clarify further. I searched Help
for the SessionEnding event. The help page titled:
"SystemEvents.SessionEnding Event" was what I was looking for.

What Mattias eludes to in his reply is in the remarks portion. I
copied the sample code, namely the WindProc Subroutine, minus the
MessageBox portion. I copied the routine and the Private Shared
variables into my form code.

In my Form's Closing event I modified it to say:

If Not bOnExit And Not systemShutdown Then
e.Cancel = True
Me.Hide()
End If

This did the trick. The reason I still need bOnExit is because the
form's close (X) should only hide the form, and only the context menu/
Exit option or system shutdown should close the form.

Note systemShutdown is a variable, see the sample code. You can access
the blank WndProc Sub by clicking on the Events drop down in Visual
Studio and choosing (Overrides) and on the right side choosing the
last subroutine, WndProc.

Again, thank you Mattias.
 

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