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

Detect when Windows is shutting down

P: n/a
I have an application to minimizes when X is clicked. If the user wants to
close the application they click the Exit Application button.This works fine
and does exactly what I need. I have since discovered it is holding Windows
open when you shutdown. If the application is running and the user
shutsdown Window it holds up everything. If the application is not running
and the user shutsdown Windows is shutsdown in a timely manner.

How can I detect when Windows is shutting down and have the application
close itself?

Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies

P: n/a

"Mike Stephens" <no************> schrieb:
How can I detect when Windows is shutting down and have
the application close itself?
-> "CloseWindow"

Herfried K. Wagner
MVP VB Classic, VB.NET
Nov 20 '05 #2

P: n/a
Hi Mike,

.NET has just the thing for you. :-)

It's incredibly simple but it took a bit of a hunt finding out how to use
it. the MSDN documentation is almost meaningless :-(

I found this site very useful:

In the Microsoft.Win32 namespace there are a set of SystemEvents two of
which cover System Shutdown - SessionEnding and SessionEnded. The first is a
request and you you have the option of cancelling the request. The second is a

To react to these events, put
AddHandler (SystemEvents.SessionEnding, AddressOf
AddHandler (SystemEvents.SessionEnded, AddressOf ShutDown.OnShutDown)
somewhere in your initialisation code.

Put the following class somewhere

Public Class Shutdown
Public Shared Sub OnShuttingdown (sender As Object, e As
e.Cancel = True 'True if you are turning down the request to
shutdown. Otherwise leave.
Console.WriteLine ("Shutting down - Reason is " & e.Reason)
'Shut down your Application here or...
End Sub

Public Shared Sub OnShutdown (sender As Object, e As
Console.WriteLine ("Shutdown - Reason is " & e.Reason)
'or...Shut down your Application here.
End Sub
End Class

Note. I haven't tested this. - too much hassle. Come back if you need any
more help with it.

Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.