让一个无形的 C# 程序在关闭/重新启动期间自行关闭?

发布于 2024-09-19 14:23:57 字数 466 浏览 4 评论 0原文

我正在编写一个程序,在需要之前根本不创建任何表单。问题是,它阻止了自动继续关闭。我看过讨论 关于添加 if 来形成关闭事件以检查是否由于关闭而导致,但正如我所说,我的程序在需要之前根本没有任何形式。

是否有任何事件或其他方法可以让我知道我的程序何时应该自行关闭以允许 Windows 自动关闭? 而且它不是多线程的

I'm writing a program which creates no forms at all until one is required. The problem is, it's preventing shutdown from continuing automatically. I've seen discussions about adding an if to form closing events to check if it's due to shutdown, but as I've said, my program is meant to have no forms at all until required.

Is there any event or some other method that will allow me to know when my program should be closing itself to allow for Windows to shut itself down automatically? And it's not multithreaded.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清音悠歌 2024-09-26 14:26:01

处理 Microsoft.Win32.SystemEvents.SessionEnding 事件,并使用 System.Environment.HasShutdownStarted 检查是否实际关闭

Handling the Microsoft.Win32.SystemEvents.SessionEnding event, and checking if it's an actual shutdown with System.Environment.HasShutdownStarted

扬花落满肩 2024-09-26 14:25:40

您始终可以添加一个最小化打开的虚拟表单,任务栏上没有图标 - 它不会有任何视觉影响,但会发送表单关闭事件 - 您可以在其中注意到关闭事件,并可能关闭/停止您的应用程序正在执行的任何其他操作。

You could always add a dummy form which you open minimized, with no icon on the taskbar - it won't have any visual impact, but will be sent the form closing event - where you could note the shutdown event, and presumably shut down/stop whatever else there is that your application is doing.

[浮城] 2024-09-26 14:25:19

您可以使用 SystemEvents 类,用于“监听”用户注销或关闭。

如果我正确理解文档(并且对 Reflector 的深入研究证实了这一点):

  • systemevents 将生成一个新线程,该线程接收来自 Windows 的消息(它有自己的消息泵)。
  • 当收到事件时,将从新线程调用您的代码。你应该意识到这一点。

You can use the SystemEvents class, to "listen to" users logging out, or shutting down.

If I understand the documentation correctly (and a deep study with Reflector confirms this):

  • The systemevents will spawn a new thread that receives the messages from windows (it has its own messagepump).
  • When an event is received, your code will be called, from the new thread. You should be aware of this.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文