Application.Exit需要调用两次
我有一个 Main 方法,它创建一个名为 SysTrayApp 的消息循环:
void Main()
{
Application.Run(new SysTrayApp());
}
我的 SysTrayApp 类将 ContextMenu 添加到系统托盘图标,然后启动另一个名为 MouseHook
的消息循环。 MouseHook 是一个调用鼠标事件的类。 (根据此类建模。)
class SysTrayApp : Form
{
public SysTrayApp()
{
// Does some GUI work
// Then starts another message loop
Application.Run(new MouseHook());
}
}
_
class MouseHook : Form
{
public MouseHook()
{
// Does some hooking stuff
}
}
在SysTrayApp, Application.Exit()
当用户从上下文菜单中选择“退出”选项时调用。问题是我必须调用 Exit 两次(或添加另一个 Application.Exit
)才能结束应用程序。这是预料之中的,但是 我觉得我处理这件事的方式是错误的。两次调用 Application.Exit()
对我来说似乎是错误的,但我是 C# 世界的新手。 SysTrayApp
和 MouseHook
是否应该合并为一个大类? MouseHook
似乎也不应该与 Windows.Form 分开。也许 MouseHook
需要位于新线程上?有什么建议吗?
编辑: 哎呀。 MouseHook
没有第三个 Application.Run()
解决方案
我想我想得太多了。正如我所说,仍在探索 C#。但这是有效的:
Main()
中的 Application.Run(new SysTrayApp());
就是您所需要的。要启动 MouseHook
,只需在 SysTrayApp
中执行 MouseHook hook = new MouseHook();
即可。
感谢@JonSkeet 帮助我理解这一点。你是一台机器!
I have a Main method that creates a message loop called SysTrayApp:
void Main()
{
Application.Run(new SysTrayApp());
}
My SysTrayApp class adds a ContextMenu to the System Tray icon, then starts another message loop called MouseHook
. MouseHook is a class that pinvokes mouse events. (Modeled after this class.)
class SysTrayApp : Form
{
public SysTrayApp()
{
// Does some GUI work
// Then starts another message loop
Application.Run(new MouseHook());
}
}
_
class MouseHook : Form
{
public MouseHook()
{
// Does some hooking stuff
}
}
In SysTrayApp, Application.Exit()
is called when the user selects the Exit option from the context menu. The problem is that I have to call Exit twice (or add another Application.Exit
) for the application to end. This is expected, but
I feel like I am going about this the wrong way. Calling Application.Exit()
twice just seems wrong to me, but I'm new to the C# world. Should SysTrayApp
and MouseHook
be combined into one big class? MouseHook
doesn't seem like it should be apart of Windows.Form either. Maybe MouseHook
needs to be on a new thread? Any suggestions?
EDIT: Oops. MouseHook
did not have a third Application.Run()
Solution
I guess I was over thinking this. Still exploring C#, like I said. But here is what works:
Application.Run(new SysTrayApp());
in Main()
is all you need. To start MouseHook
, just do a MouseHook hook = new MouseHook();
in SysTrayApp
.
Thanks @JonSkeet for helping me understand this. You are a machine!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在
MouseHook
构造函数中调用Application.Run
,然后当它返回时,您将调用Application.Run code> 再次(在
SysTrayApp
构造函数中) - 然后,当SysTrayApp
构造函数完成时,您将第三次调用它。如果您要运行三个不同的消息循环,则应该预计必须退出三次(我很惊讶您只需要调用Application.Exit
两次)。您不应该在您的构造函数中调用
Application.Run
- 在我看来,构造函数阻塞直到消息泵完成的想法是令人讨厌的。如果您确实想要多个消息循环,它们应该位于不同的线程上 - 但我不建议您这样做。您只需在Main
方法中调用Application.Run
就可以了。You're calling
Application.Run
in theMouseHook
constructor and then when that returns, you're callingApplication.Run
again (in theSysTrayApp
constructor) - you're then calling it a third time when theSysTrayApp
constructor completes. If you're going to run three different message loops, you should expect to have to exit three times (I'm amazed you're only having to callApplication.Exit
twice).You shouldn't be calling
Application.Run
in either of your constructors - the idea of a constructor blocking until a message pump has finishes is nasty, IMO. If you really want multiple message loops, they should be on different threads - but I wouldn't advise you to do that. You should be fine just callingApplication.Run
in yourMain
method.