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

Home Posts Topics Members FAQ

Application doesn't close on Shut Down - Please help!

Hi Guys,

I have a small application which uses a NotifyIcon. The user can set a
flag (PreventClosing ) so that when he clicks on the form-cancel button
"X", the program will instead be minimized in the icon try. See code
attached.

My problem is that my code is probably too naive. In fact there is a
problem when one tries to shut dow or restart Windows. Windows does NOT
want to shut down! I guess it's the e.cancel = TRUE statement in the
closing handler that prevents it.

Could anybody by so kind as to suggest me the correct approach to do
this. Is there a way to detect the the closing is due to system restart
or shutdown?

Thank you very much in advance,

-Pam

---------------------------------------------------------------------------*--

Private PreventClosing As Boolean
'if this is set TRUE by the user, the program minimizes (instead of
closing) when "X" is pressed
Private ShowInTaskbarSa ved As Boolean
Private WindowStateSave d As FormWindowState
'...
Private Sub Form1_Closing(B yVal sender As Object, ByVal e As
System.Componen tModel.CancelEv entArgs) Handles MyBase.Closing
If Me.PreventClosi ng Then
Me.HideInTrayIc on()
e.Cancel = True
End If
End Sub

Private Sub HideInTrayIcon( )
Me.WindowStateS aved = Me.WindowState
Me.WindowState = FormWindowState .Minimized
Me.ShowInTaskba r = False
Me.NotifyIcon1. Visible = True
End Sub

'...
Private Sub RestoreWindow()
Me.NotifyIcon1. Visible = False
Me.ShowInTaskba r = Me.ShowInTaskba rSaved
Me.WindowState = Me.WindowStateS aved
Me.Show()
Me.Focus()
End Sub

----------------------

thank you in advance

-Pam

Jan 23 '06 #1
4 3697
pa***********@l ibero.it wrote:
Hi Guys,

I have a small application which uses a NotifyIcon. The user can set a
flag (PreventClosing ) so that when he clicks on the form-cancel button
"X", the program will instead be minimized in the icon try. See code
attached.

My problem is that my code is probably too naive. In fact there is a
problem when one tries to shut dow or restart Windows. Windows does NOT
want to shut down! I guess it's the e.cancel = TRUE statement in the
closing handler that prevents it.

Could anybody by so kind as to suggest me the correct approach to do
this. Is there a way to detect the the closing is due to system restart
or shutdown?

Thank you very much in advance,

-Pam

---------------------------------------------------------------------------*--

Private PreventClosing As Boolean
'if this is set TRUE by the user, the program minimizes (instead of
closing) when "X" is pressed
Private ShowInTaskbarSa ved As Boolean
Private WindowStateSave d As FormWindowState
'...
Private Sub Form1_Closing(B yVal sender As Object, ByVal e As
System.Componen tModel.CancelEv entArgs) Handles MyBase.Closing
If Me.PreventClosi ng Then
Me.HideInTrayIc on()
e.Cancel = True
End If
End Sub

Private Sub HideInTrayIcon( )
Me.WindowStateS aved = Me.WindowState
Me.WindowState = FormWindowState .Minimized
Me.ShowInTaskba r = False
Me.NotifyIcon1. Visible = True
End Sub

'...
Private Sub RestoreWindow()
Me.NotifyIcon1. Visible = False
Me.ShowInTaskba r = Me.ShowInTaskba rSaved
Me.WindowState = Me.WindowStateS aved
Me.Show()
Me.Focus()
End Sub

----------------------

thank you in advance

-Pam


You may want to look at this site. It has info on handling win32 events.

http://www.codeworks.it/net/Sysevents.htm

You should be able to use it to get notified that the system (or
session) wants to shutdown, and close your program.
Never tried it, but in my head, it should work.
Chris
Jan 23 '06 #2
Dear Chris,

Thank you VERY much for your suggestion. I have read the
implementation, trying to do what you seems to suggest (hope I have
correctly interpreted).
See attached code changes.

There is however a big problem. The CLOSING event is called BEFORE
SystemEvents.Se ssionEnding. So the System Shut Down is blocked before I
can detect the session is being shutting down! :-(

What can I do ? Thank you very much for your patience !!

-Pam

--------------

Imports Microsoft.Win32
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

'....
AddHandler SystemEvents.Se ssionEnding, AddressOf
Me.SessionEndin g

End Sub
Private Sub Form1_Closing(B yVal sender As Object, ByVal e As
System.Componen tModel.CancelEv entArgs) Handles MyBase.Closing

MsgBox("Program Closing") 'here we get!

If Not Me.SessionIsEnd ing AndAlso Me.PreventClosi ng Then
Me.HideInTrayIc on()
e.Cancel = True
Else
Me.NotifyIcon1. Visible = False
End If

End Sub

Private SessionIsEnding As Boolean

Public Sub SessionEnding(B yVal sender As Object, ByVal e As
SessionEndingEv entArgs)
MsgBox("Session Ending") 'never get here :-(
Me.SessionIsEnd ing = True
End Sub
--------------------------------------------

thank you very much,

-Pam

Jan 23 '06 #3
Del
I answered this question a few days ago. Of couse you can ommit the
MessageBox & just let your application close on Windows shutdown

Private Shared WM_QUERYENDSESS ION As Integer = &H11
Private Shared systemShutdown As Boolean = False
Protected Overrides Sub WndProc(ByRef m As System.Windows. Forms.Message)
If m.Msg = WM_QUERYENDSESS ION Then
MessageBox.Show ("queryendsessi on: this is a logoff, shutdown, or
reboot")
systemShutdown = True
End If
' If this is WM_QUERYENDSESS ION, the closing event should be fired in
the base WndProc
MyBase.WndProc( m)
End Sub 'WndProc
Private Sub Form1_Closing(B yVal sender As System.Object, ByVal e As
System.Componen tModel.CancelEv entArgs) Handles MyBase.Closing
If (systemShutdown ) Then
' reset the variable since they may cancel the shutdown
systemShutdown = False
If (DialogResult.Y es = _
MessageBox.Show ("My application", "Would you care to save
your work before logging off?", MessageBoxButto ns.YesNo)) Then
e.Cancel = True
Else
e.Cancel = False
End If
End If
End Sub

I hope that helps

Crouchie1998
BA (HONS) MCP MCSE
Jan 24 '06 #4

Thank you VERY VERY much Del !! It works perfectly and is exactly what
I needed.

Del ha scritto:
I answered this question a few days ago. Of couse you can ommit the
MessageBox & just let your application close on Windows shutdown
Ah. Please, excuse me. I can't read this fantastic group regularly
Private Shared WM_QUERYENDSESS ION As Integer = &H11 .... I hope that helps
Very much!

Crouchie1998
BA (HONS) MCP MCSE


See you soon.

-Pam

Jan 24 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2035
by: downwitch | last post by:
Greetings, I have an application which has recently gone from being one big piece of front-end code to being three, i.e. an mdb A that references an mda B and an mda C. (For what it's worth, in the interest of complete disclosure, mda B also references mda C). This application has always closed in the mvps time-honored method of calling Application.Quit on the Unload event of the switchboard.
3
3481
by: kuldeep | last post by:
Hi all, I have a application developed in C# .net. The problem I face is I am unable to shutdown my machine when the exe is running. Windows is unable to close this exe an shut down. Can anyone help. Regards, Kuldeep Pawar
3
2950
by: Claire | last post by:
Windows refuses to close down if my applications main form is hidden and there's a notify icon in the system tray. If I restore the form, then shutting down Windows succeeds. If I comment out the code in "frmOptions_Closing" then run and hide the application, Windows shuts down normally. I think there was a way to find out what was causing an application to close down - api call? Is there anything in .net that I can query My code is...
4
1891
by: andersboth | last post by:
I want to run some code when my Windows Form Application is being shut down. I want to run this code when the Application is being shut down in the following situation: 1. The App is being shut down by the user. 2. The App is being shut down because of Windows System Shut Down. 3. The App Process is being killed in Task Manager
1
2709
by: Manoj Nair | last post by:
Hi, The problem : Have a system tray application.This has a menu item 'Exit'. On click of this a differnt application with a UI which runs in the background should close. The other application requires a Keyboard combination of Ctrl + Shift + Alt + X to close it properly. After closing the other application the tray app should exit. (Process.Kill cannot be used)
5
1825
by: James Radke | last post by:
Hello, I am using some external functions supplied by an ERP vendor in a windows.forms application (vb.net, visual studio.net 2003) The functions are defined as follows: ' Max startup and shutdown functions Declare Function InitMAXOrder Lib "MAXORDR2.DLL" (ByRef DataPath As StringBuilder, ByRef MAXPath As StringBuilder) As Integer
6
1207
by: marcmc | last post by:
I have an icon that points at a server application written in dotNet. I wish to allow the users on distributed pc's to be able to close the application and walk away but I have to ensure that this does not shut down the actual application on the server. In this way the application on the server acts kind of like a service but it can never be closed down on the server. Has anybody any idea how to implement this or indeed can it be...
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.
1
2845
by: G Gerard | last post by:
Hello I have written some help files using Html Help Workshop version 4.74.8702 I use the following code to launch the chm file Declare Function HtmlHelp Lib "HHCtrl.ocx" Alias "HtmlHelpA" _ (ByVal hwndCaller as long, ByVal pszFile as String, ByVal uCommand as long, dwData as Any) as Long
0
9617
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9453
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
10099
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10036
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
8929
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
7451
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...
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3607
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.