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

How can you detect application close event with no form?

P: n/a
I have an vb.net application that is a module that uses a "application.run"
in the sub main to start. There is no form involved (just a system tray
icon)
How can you detect when the application is being closed? It is easy enough
if the user selects "exit" from this tray icon but how can you detect if
Windows is closing the program down? Normally I would simply do something in
the Form.Closing event but without a form how can you determine that the
application is being closed by Windows?
Nov 21 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Just call Application.Exit

"Patrick Dugan" <pa***********@usnetNOSMORESPAMcomcorp.com> wrote in message
news:et****************@TK2MSFTNGP09.phx.gbl...
I have an vb.net application that is a module that uses a "application.run"
in the sub main to start. There is no form involved (just a system tray
icon)
How can you detect when the application is being closed? It is easy
enough if the user selects "exit" from this tray icon but how can you
detect if Windows is closing the program down? Normally I would simply do
something in the Form.Closing event but without a form how can you
determine that the application is being closed by Windows?

Nov 21 '05 #2

P: n/a
Application.Exit simply exits the program. I want to know how to detect
that Windows is telling my application to shut down. For example when
Windows is shutting down or restarting it will close all running
applications. I want to know how to detect that event when there is no form
in the application.
"Marina" <so*****@nospam.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Just call Application.Exit

"Patrick Dugan" <pa***********@usnetNOSMORESPAMcomcorp.com> wrote in
message news:et****************@TK2MSFTNGP09.phx.gbl...
I have an vb.net application that is a module that uses a
"application.run" in the sub main to start. There is no form involved
(just a system tray icon)
How can you detect when the application is being closed? It is easy
enough if the user selects "exit" from this tray icon but how can you
detect if Windows is closing the program down? Normally I would simply do
something in the Form.Closing event but without a form how can you
determine that the application is being closed by Windows?


Nov 21 '05 #3

P: n/a

a long time ago, fergus suggested this to me...and it works just fine...

somewhere in your initialisation code:

either event will work just fine, but read the differences so you know which
is most appropriate for what you want to accomplish.

AddHandler ( _
SystemEvents.SessionEnding, _
AddressOf ShutDown.OnShuttingDown _
)
AddHandler ( _
SystemEvents.SessionEnded, _
AddressOf ShutDown.OnShutDown _
)

put the following class somewhere

Public Class ShutDown
Public Shared Sub OnShuttingdown( _
sender As Object, _
e As SessionEndingEventArgs _
)
e.Cancel = True 'only set if you turn down the shutdown request
Console.WriteLine ("Shutting down - Reason is " & e.Reason)
' your code here to clean up before exiting
End Sub

Public Shared Sub OnShutdown ( _
sender As Object, _
e As SessionEndedEventArgs _
)
Console.WriteLine ("Shutdown - Reason is " & e.Reason)
' your code here to clean up before exiting
End Sub
End Class

i'll be happy to help you if you can't figure out how to incorporate this
snippet into your application.

hth,

me

Nov 21 '05 #4

P: n/a

That looks good but the "AddHandler" lines simply say "syntax error" in
vb.net. I've added them to the top under the variable declarations and
within the sub main but they always show syntax error.
"" <a@b.com> wrote in message news:lm******************@fe05.lga...
a long time ago, fergus suggested this to me...and it works just fine...

somewhere in your initialisation code:

either event will work just fine, but read the differences so you know
which
is most appropriate for what you want to accomplish.

AddHandler ( _
SystemEvents.SessionEnding, _
AddressOf ShutDown.OnShuttingDown _
)
AddHandler ( _
SystemEvents.SessionEnded, _
AddressOf ShutDown.OnShutDown _
)

put the following class somewhere

Public Class ShutDown
Public Shared Sub OnShuttingdown( _
sender As Object, _
e As SessionEndingEventArgs _
)
e.Cancel = True 'only set if you turn down the shutdown request
Console.WriteLine ("Shutting down - Reason is " & e.Reason)
' your code here to clean up before exiting
End Sub

Public Shared Sub OnShutdown ( _
sender As Object, _
e As SessionEndedEventArgs _
)
Console.WriteLine ("Shutdown - Reason is " & e.Reason)
' your code here to clean up before exiting
End Sub
End Class

i'll be happy to help you if you can't figure out how to incorporate this
snippet into your application.

hth,

me

Nov 21 '05 #5

P: n/a
Ignore my previous post. I figured out where to place them. Thanks!

"" <a@b.com> wrote in message news:lm******************@fe05.lga...
a long time ago, fergus suggested this to me...and it works just fine...

somewhere in your initialisation code:

either event will work just fine, but read the differences so you know
which
is most appropriate for what you want to accomplish.

AddHandler ( _
SystemEvents.SessionEnding, _
AddressOf ShutDown.OnShuttingDown _
)
AddHandler ( _
SystemEvents.SessionEnded, _
AddressOf ShutDown.OnShutDown _
)

put the following class somewhere

Public Class ShutDown
Public Shared Sub OnShuttingdown( _
sender As Object, _
e As SessionEndingEventArgs _
)
e.Cancel = True 'only set if you turn down the shutdown request
Console.WriteLine ("Shutting down - Reason is " & e.Reason)
' your code here to clean up before exiting
End Sub

Public Shared Sub OnShutdown ( _
sender As Object, _
e As SessionEndedEventArgs _
)
Console.WriteLine ("Shutdown - Reason is " & e.Reason)
' your code here to clean up before exiting
End Sub
End Class

i'll be happy to help you if you can't figure out how to incorporate this
snippet into your application.

hth,

me

Nov 21 '05 #6

P: n/a

np...let me know how it works.
"Patrick Dugan" <pa***********@usnetNOSMORESPAMcomcorp.com> wrote in message
news:Ot**************@TK2MSFTNGP09.phx.gbl...
| Ignore my previous post. I figured out where to place them. Thanks!
|
| "" <a@b.com> wrote in message news:lm******************@fe05.lga...
| >a long time ago, fergus suggested this to me...and it works just fine...
| >
| > somewhere in your initialisation code:
| >
| > either event will work just fine, but read the differences so you know
| > which
| > is most appropriate for what you want to accomplish.
| >
| > AddHandler ( _
| > SystemEvents.SessionEnding, _
| > AddressOf ShutDown.OnShuttingDown _
| > )
| > AddHandler ( _
| > SystemEvents.SessionEnded, _
| > AddressOf ShutDown.OnShutDown _
| > )
| >
| > put the following class somewhere
| >
| > Public Class ShutDown
| > Public Shared Sub OnShuttingdown( _
| > sender As Object, _
| > e As SessionEndingEventArgs _
| > )
| > e.Cancel = True 'only set if you turn down the shutdown request
| > Console.WriteLine ("Shutting down - Reason is " & e.Reason)
| > ' your code here to clean up before exiting
| > End Sub
| >
| > Public Shared Sub OnShutdown ( _
| > sender As Object, _
| > e As SessionEndedEventArgs _
| > )
| > Console.WriteLine ("Shutdown - Reason is " & e.Reason)
| > ' your code here to clean up before exiting
| > End Sub
| > End Class
| >
| > i'll be happy to help you if you can't figure out how to incorporate
this
| > snippet into your application.
| >
| > hth,
| >
| > me
| >
| >
| >
|
|
Nov 21 '05 #7

P: n/a
I rebooted my computer to test it but it works great! That was exactly what
I needed!
Thanks!
"" <a@b.com> wrote in message news:2I******************@fe05.lga...
np...let me know how it works.
"Patrick Dugan" <pa***********@usnetNOSMORESPAMcomcorp.com> wrote in
message
news:Ot**************@TK2MSFTNGP09.phx.gbl...
| Ignore my previous post. I figured out where to place them. Thanks!
|
| "" <a@b.com> wrote in message news:lm******************@fe05.lga...
| >a long time ago, fergus suggested this to me...and it works just
fine...
| >
| > somewhere in your initialisation code:
| >
| > either event will work just fine, but read the differences so you know
| > which
| > is most appropriate for what you want to accomplish.
| >
| > AddHandler ( _
| > SystemEvents.SessionEnding, _
| > AddressOf ShutDown.OnShuttingDown _
| > )
| > AddHandler ( _
| > SystemEvents.SessionEnded, _
| > AddressOf ShutDown.OnShutDown _
| > )
| >
| > put the following class somewhere
| >
| > Public Class ShutDown
| > Public Shared Sub OnShuttingdown( _
| > sender As Object, _
| > e As SessionEndingEventArgs _
| > )
| > e.Cancel = True 'only set if you turn down the shutdown request
| > Console.WriteLine ("Shutting down - Reason is " & e.Reason)
| > ' your code here to clean up before exiting
| > End Sub
| >
| > Public Shared Sub OnShutdown ( _
| > sender As Object, _
| > e As SessionEndedEventArgs _
| > )
| > Console.WriteLine ("Shutdown - Reason is " & e.Reason)
| > ' your code here to clean up before exiting
| > End Sub
| > End Class
| >
| > i'll be happy to help you if you can't figure out how to incorporate
this
| > snippet into your application.
| >
| > hth,
| >
| > me
| >
| >
| >
|
|

Nov 21 '05 #8

P: n/a

great...always glad to pass along a good tip...thanks to fergus for pointing
out the systemevents class and the associated snippet.
"Patrick Dugan" <pa***********@usnetNOSMORESPAMcomcorp.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
|I rebooted my computer to test it but it works great! That was exactly
what
| I needed!
| Thanks!
|
|
| "" <a@b.com> wrote in message news:2I******************@fe05.lga...
| > np...let me know how it works.
| >
| >
| > "Patrick Dugan" <pa***********@usnetNOSMORESPAMcomcorp.com> wrote in
| > message
| > news:Ot**************@TK2MSFTNGP09.phx.gbl...
| > | Ignore my previous post. I figured out where to place them. Thanks!
| > |
| > | "" <a@b.com> wrote in message news:lm******************@fe05.lga...
| > | >a long time ago, fergus suggested this to me...and it works just
| > fine...
| > | >
| > | > somewhere in your initialisation code:
| > | >
| > | > either event will work just fine, but read the differences so you
know
| > | > which
| > | > is most appropriate for what you want to accomplish.
| > | >
| > | > AddHandler ( _
| > | > SystemEvents.SessionEnding, _
| > | > AddressOf ShutDown.OnShuttingDown _
| > | > )
| > | > AddHandler ( _
| > | > SystemEvents.SessionEnded, _
| > | > AddressOf ShutDown.OnShutDown _
| > | > )
| > | >
| > | > put the following class somewhere
| > | >
| > | > Public Class ShutDown
| > | > Public Shared Sub OnShuttingdown( _
| > | > sender As Object, _
| > | > e As SessionEndingEventArgs _
| > | > )
| > | > e.Cancel = True 'only set if you turn down the shutdown request
| > | > Console.WriteLine ("Shutting down - Reason is " & e.Reason)
| > | > ' your code here to clean up before exiting
| > | > End Sub
| > | >
| > | > Public Shared Sub OnShutdown ( _
| > | > sender As Object, _
| > | > e As SessionEndedEventArgs _
| > | > )
| > | > Console.WriteLine ("Shutdown - Reason is " & e.Reason)
| > | > ' your code here to clean up before exiting
| > | > End Sub
| > | > End Class
| > | >
| > | > i'll be happy to help you if you can't figure out how to incorporate
| > this
| > | > snippet into your application.
| > | >
| > | > hth,
| > | >
| > | > me
| > | >
| > | >
| > | >
| > |
| > |
| >
| >
|
|
Nov 21 '05 #9

P: n/a
"Patrick Dugan" <pa***********@usnetNOSMORESPAMcomcorp.com> schrieb:
That looks good but the "AddHandler" lines simply say "syntax error" in
vb.net. I've added them to the top under the variable declarations and
within the sub main but they always show syntax error.
[...]
AddHandler ( _
SystemEvents.SessionEnding, _
AddressOf ShutDown.OnShuttingDown _
)


Remove the braces ('(', ')').

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #10

P: n/a


"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:un**************@TK2MSFTNGP12.phx.gbl...
| "Patrick Dugan" <pa***********@usnetNOSMORESPAMcomcorp.com> schrieb:
| > That looks good but the "AddHandler" lines simply say "syntax error" in
| > vb.net. I've added them to the top under the variable declarations and
| > within the sub main but they always show syntax error.
| > [...]
| >> AddHandler ( _
| >> SystemEvents.SessionEnding, _
| >> AddressOf ShutDown.OnShuttingDown _
| >> )
|
| Remove the braces ('(', ')').

once again herf, it is you who needs to remove something...your head from
your ass. the syntax is NOT a problem. if you knew what the hell you were
talking about, you know the problem was *where* the op placed this code.

why are you an "mvp" again?
Nov 21 '05 #11

P: n/a
"" <a@b.com> schrieb:
| > That looks good but the "AddHandler" lines simply say "syntax error"
in
| > vb.net. I've added them to the top under the variable declarations
and
| > within the sub main but they always show syntax error.
| > [...]
| >> AddHandler ( _
| >> SystemEvents.SessionEnding, _
| >> AddressOf ShutDown.OnShuttingDown _
| >> )
|
| Remove the braces ('(', ')').

once again herf, it is you who needs to remove something...your head from
your ass. the syntax is NOT a problem. if you knew what the hell you were
talking about, you know the problem was *where* the op placed this code.


Again wrong. The code cannot be compiled because of the braces. You are
discrediting yourself.

From the VB.NET Language Specification:

| AddHandlerStatement ::=
| AddHandler Expression , Expression StatementTerminator
| RemoveHandlerStatement ::=
| RemoveHandler Expression , Expression StatementTerminator

BTW:

Rules of Conduct
<URL:http://www.microsoft.com/communities/conduct/default.mspx>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #12

P: n/a


"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:Ox**************@TK2MSFTNGP12.phx.gbl...

| Rules of Conduct
| <URL:http://www.microsoft.com/communities/conduct/default.mspx>

Rules of Conduct

Appropriate Language: The purpose of our communities is to exchange
technical information and expertise about Microsoft products. Please avoid
personal attacks, slurs, and profanity in your interactions.

fuck you, herf...you dumbass, third-world, wannabe programmer.

Relevance to Topics: Please make sure that your postings in newsgroups and
chats are relevant to the subject at hand. It is normal for some topics to
drift from the stated subject. However, to ensure maximum benefit for
everyone, we encourage you to keep your postings as close to the subject as
possible.

today, the sky is blue...pull your head out of you ass, herf, and maybe take
a look at it.

Advertising/Solicitation: These communities were created as a forum for
providing peer-to-peer assistance related to using Microsoft products and
services. We ask that you refrain from posting unsolicited advertisements
that do not pertain directly to the intended use and purpose of the
newsgroup or chat.

for the best kiddie-porn in town: mailto:hi**@qmx.at

Confidentiality: Please keep in mind that our communities are public spaces,
so don't post anything that you don't want the world to see. Credit card
numbers, product keys, and other confidential information, including
anything covered under a non-disclosure agreement (NDA), should not be
posted to a newsgroup, chat, or other community.

herf is one big nda for stupidity...that's ok, you can keep those secrets to
yourself as i don't care to have your special brand thereof replicated. and
now that i think of it, please don't involve yourself in copulation
either...the results could be disastrous.
Nov 21 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.