Closing a notifyicon application

  • Thread starter Thread starter Zamdrist
  • Start date Start date
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...
 
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
 
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.
 
Back
Top