如何检测没有表格的应用程序关闭事件? [英] How can you detect application close event with no form?

查看:46
本文介绍了如何检测没有表格的应用程序关闭事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个vb.net应用程序,它是一个模块,它使用子主程序中的application.run

来启动。没有涉及的形式(只是一个系统托盘

图标)

如何检测应用程序何时关闭?如果用户选择退出,则很容易

。从这个托盘图标,但你怎么能检测到Windows是否正在关闭程序?通常我会在

的Form.Closing事件中做一些事情,但是如果没有表单你怎么能确定Windows关闭了

应用程序?

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?

推荐答案

只需致电Application.Exit


" Patrick Dugan" < PA *********** @ usnetNOSMORESPAMcomcorp.com>在留言中写道

news:et **************** @ TK2MSFTNGP09.phx.gbl ...
Just call Application.Exit

"Patrick Dugan" <pa***********@usnetNOSMORESPAMcomcorp.com> wrote in message
news:et****************@TK2MSFTNGP09.phx.gbl...
我有一个vb.net应用程序是一个模块,它使用子主程序中的application.run
来启动。没有涉及的形式(只是一个系统托盘
图标)
如何检测应用程序何时关闭?如果用户选择退出,则足够容易。从这个托盘图标,但你怎么能检测Windows是否关闭程序?通常情况下我会在Form.Closing事件中做一些事情但没有表格你怎么能确定应用程序被Windows关闭?
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?



Application.Exit只退出程序。我想知道如何检测Windows告诉我的应用程序关闭的问题。例如,当Windows关闭或重新启动时,它将关闭所有运行的

应用程序。我想知道如何在应用程序中没有形式

时检测到该事件。

Marina" <所以***** @ nospam.com>在留言中写道

news:%2 **************** @ tk2msftngp13.phx.gbl ...
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...
只需致电Application.Exit

Patrick Dugan < PA *********** @ usnetNOSMORESPAMcomcorp.com>在
消息新闻中写道:et **************** @ TK2MSFTNGP09.phx.gbl ...
Just call Application.Exit

"Patrick Dugan" <pa***********@usnetNOSMORESPAMcomcorp.com> wrote in
message news:et****************@TK2MSFTNGP09.phx.gbl...
我有一个vb.net应用程序这是一个使用
application.run的模块。在子主要开始。没有涉及的形式
(只是系统托盘图标)
如何检测应用程序何时关闭?如果用户选择退出,则足够容易。从这个托盘图标,但你怎么能检测Windows是否关闭程序?通常情况下我会在Form.Closing事件中做一些事情但没有表格你怎么能确定应用程序被Windows关闭?
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?




很久以前,fergus向我建议......它工作得很好......


在你的某个地方初始化代码:


任一事件都可以正常工作,但要阅读差异,以便了解哪个

最适合您想要完成的事情。 />

AddHandler(_

SystemEvents.SessionEnding,_

AddressOf ShutDown.OnShuttingDown _



AddHandler(_

SystemEvents.SessionEnded,_

AddressOf ShutDown.OnShutDown _




将以下课程放在某个地方


公共课程ShutDown

公共共享子OnShuttingdown(_

寄给者As对象,_
$ b $ as As SessionEndingEventArgs _



e.Cancel = True''仅在您关闭关闭请求时设置

Console.WriteLine(" Shutting down - Reason is" &安培; e.Reason)

''你的代码在这里清理之前要退出

End Sub


Public Shared Sub OnShutdown(_

发件人作为对象,_
$ b $ as As SessionEndedEventArgs _



Console.WriteLine(" Shutdown - Reason是& e.Reason

''你的代码在这里清理之前要退出

End Sub

End Class


如果您无法弄清楚如何将这个

片段整合到您的应用程序中,我将很乐意为您提供帮助。

hth,


me

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


这篇关于如何检测没有表格的应用程序关闭事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆