473,770 Members | 1,980 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 19936
Hello,

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


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

--
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.S essionEnding, AddressOf
ShutDown.OnShut tingDown)
AddHandler (SystemEvents.S essionEnded, AddressOf ShutDown.OnShut Down)
somewhere in your initialisation code.

Put the following class somewhere

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

Public Shared Sub OnShutdown (sender As Object, e As
SessionEndedEve ntArgs)
Console.WriteLi ne ("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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
2463
by: Nancy Drew | last post by:
hi all i'm trying to keep users from being able to login to a site twice at the same time. everytime a user does a login, i stick their userID into an application scoped array. if they try to login again, i bounce them to an error page. i use the session_onEnd sub within global.asa to remove their userID from the array at the end of their session, and this seems to work fine. however, if i just shut down the browser, the sub_onEnd...
10
18658
by: Jesper | last post by:
Does anyone know some c++ code for shutting down windows? I want to write a program using borland c++ builder to shut down windows. --
7
2566
by: ljlevend | last post by:
Is there any way (either .NET or API) to detect when Windows.Forms.Form.ActiveForm changes? Thanks, Lance
0
1097
by: Sidney | last post by:
Dear, I am writting a system which opened at the system tray. But every before I shut down my computer, I have to close the system first. However, it should be automatically close when I shutting down my system. So, I want to know how I can do so that it can close up itself when the computer shut down. Thank You!! Sidney
4
3408
by: Markus Stoeger | last post by:
Hi, I have a problem with Application.Run() when Windows is shutting down. Please have a look at the copy&paste example program below. The application has no forms. It has only got a notify icon in the system tray and it uses Application.Run() to keep the message loop running. When the user clicks the icon, the application should shut down and exit. So far that works fine.
7
4270
by: Mark | last post by:
It is possible from a .NET application to prevent Windows from shutting down? I understand that a .NET application can "know" that windows is initiating the process of to shutting down - but what about *preventing* that shutdown? (and yes - I know the user can always unplug the machine) Thanks.
3
3182
by: leocwh | last post by:
Dear all, I would like to know how to run the execuatable before windows shutting down. Here is my simple code: Private Sub Command1_Click() Shell "C:\abc.bat", vbNormalFocus End Sub Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
1
1209
by: Sebouh | last post by:
Hi guys. I''m curious about what happens when windows shuts down. I have a program running that saves data into a file when the form's closing method is invoked. So when Windows shuts down, does it call this method? Plus, i need to implement the method in a way that, when the user clicks the X button on the form, the program minimizes and a notify icon appears. The problem is if i call e.cancel, to cancel the close, Windows doesn't shut...
2
2021
by: =?Utf-8?B?UGF1bA==?= | last post by:
Hi I am monitoring web apps with the windows event log and noticed several (Hosting environment is shutting down) statements. The server appears to still be running, just wondering any ideas on what can cause this, thanks. -- Paul G Software engineer.
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10259
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9906
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.