472,122 Members | 1,576 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,122 software developers and data experts.

Detect when Windows is shutting down

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?

Regards,
Mike.
Nov 20 '05 #1
2 19691
Hello,

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


http://www.mvps.org/dotnet/dotnet/sa...ndowsandforms/
-> "CloseWindow"

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet
Nov 20 '05 #2
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:
http://www.codeworks.it/net/Sysevents.htm

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

To react to these events, put
AddHandler (SystemEvents.SessionEnding, AddressOf
ShutDown.OnShuttingDown)
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
SessionEndingEventArgs)
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
SessionEndedEventArgs)
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.

Regards
Fergus
Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Jesper | last post: by
7 posts views Thread by ljlevend | last post: by
2 posts views Thread by =?Utf-8?B?UGF1bA==?= | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.