473,400 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 ShowInTaskbarSaved As Boolean
Private WindowStateSaved As FormWindowState
'...
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If Me.PreventClosing Then
Me.HideInTrayIcon()
e.Cancel = True
End If
End Sub

Private Sub HideInTrayIcon()
Me.WindowStateSaved = Me.WindowState
Me.WindowState = FormWindowState.Minimized
Me.ShowInTaskbar = False
Me.NotifyIcon1.Visible = True
End Sub

'...
Private Sub RestoreWindow()
Me.NotifyIcon1.Visible = False
Me.ShowInTaskbar = Me.ShowInTaskbarSaved
Me.WindowState = Me.WindowStateSaved
Me.Show()
Me.Focus()
End Sub

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

thank you in advance

-Pam

Jan 23 '06 #1
4 3667
pa***********@libero.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 ShowInTaskbarSaved As Boolean
Private WindowStateSaved As FormWindowState
'...
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If Me.PreventClosing Then
Me.HideInTrayIcon()
e.Cancel = True
End If
End Sub

Private Sub HideInTrayIcon()
Me.WindowStateSaved = Me.WindowState
Me.WindowState = FormWindowState.Minimized
Me.ShowInTaskbar = False
Me.NotifyIcon1.Visible = True
End Sub

'...
Private Sub RestoreWindow()
Me.NotifyIcon1.Visible = False
Me.ShowInTaskbar = Me.ShowInTaskbarSaved
Me.WindowState = Me.WindowStateSaved
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.SessionEnding. 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(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'....
AddHandler SystemEvents.SessionEnding, AddressOf
Me.SessionEnding

End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

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

If Not Me.SessionIsEnding AndAlso Me.PreventClosing Then
Me.HideInTrayIcon()
e.Cancel = True
Else
Me.NotifyIcon1.Visible = False
End If

End Sub

Private SessionIsEnding As Boolean

Public Sub SessionEnding(ByVal sender As Object, ByVal e As
SessionEndingEventArgs)
MsgBox("SessionEnding") 'never get here :-(
Me.SessionIsEnding = 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_QUERYENDSESSION As Integer = &H11
Private Shared systemShutdown As Boolean = False
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_QUERYENDSESSION Then
MessageBox.Show("queryendsession: this is a logoff, shutdown, or
reboot")
systemShutdown = True
End If
' If this is WM_QUERYENDSESSION, the closing event should be fired in
the base WndProc
MyBase.WndProc(m)
End Sub 'WndProc
Private Sub Form1_Closing(ByVal sender As System.Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If (systemShutdown) Then
' reset the variable since they may cancel the shutdown
systemShutdown = False
If (DialogResult.Yes = _
MessageBox.Show("My application", "Would you care to save
your work before logging off?", MessageBoxButtons.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_QUERYENDSESSION 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
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...
3
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...
3
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...
4
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...
1
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...
5
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...
6
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...
4
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...
1
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" _...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.