问题描述
大家好.我正在尝试了解如何在控制台应用程序中处理事件.我宁愿不使用静默 WinForms(尽管我知道这是一种方法)来做到这一点.我已经阅读了一个类似的问题及其回复.请参阅下面的响应文本(链接):p><块引用>
一个STA线程的基本要求是它需要运行一条消息泵.在 Windows 窗体中,您可以使用应用程序运行.或者你可以写手动消息泵,使用user32!GetMessage &调度消息.但它可能更容易使用WinForms 或 WPF 中的一个.
使用user32 -> GetMessage"的程序的基本结构是什么&user32 -> DispatchMessage"?
参见MSDN中的使用消息和消息队列"主题(在Win32和COM开发>用户界面>Windows用户体验>Windows管理>Windows用户界面>窗口 > 消息和消息队列;您可能需要查看同一部分中的其他文章和示例).快速总结,省略错误处理并使用 C 语法而不是 C#,原因如下:
RegisterClass(...);创建窗口(...);显示窗口(...);//可能不是你的情况while (GetMessage(&msg, NULL, 0, 0)) {TranslateMessage(&msg);DispatchMessage(&msg);}
从窗口设置样板中可以看出,这仍然依赖于静默窗口",尽管它是通过 Win32 API 而不是通过 WinForms 创建和发送消息的.所以你这样做并没有真正获得任何东西.因此,我觉得将这些东西翻译成 C# 并没有多大意义——如果您的问题的唯一解决方案是一个不可见的窗口,那么您不妨使用一个不可见的 Windows 窗体以及该平台附带的所有友好包装器.
但是,如果您实际上并没有像链接问题的海报那样使用 Windows 窗体控件,那么您可以非常高兴地在控制台应用程序中使用 .NET 事件.对 STA 的限制和对消息泵的需求特定于从 WinForms 和 ActiveX 控件(如 WebBrowser)接收事件(或来自 Win32 HWND 的消息,尽管这不一定需要 STA).
Hey all. I'm trying to see about handling events in a console application. I would prefer to not use silent WinForms (though I understand that that's one way) to do it. I've read over a similar question and its response. See response text below (link):
The basic requirement of an STA thread is that it needs to run a message pump. In Windows Forms, you can use Application.Run. Or you could write the message pump by hand, using user32!GetMessage & DispatchMessage. But it's probably easier to use the one in WinForms or WPF.
What the basic structure of a program that uses "user32 -> GetMessage" & "user32 -> DispatchMessage"?
See the topic "Using Messages and Message Queues" in MSDN (under Win32 and COM Development > User Interface > Windows User Experience > Windows Management > Windows User Interface > Windowing > Messages and Message Queues; you'll probably need to take a look at the other articles and samples in the same section). Quick summary, omitting error handling and using C syntax rather than C# for reasons discussed below:
RegisterClass(...);
CreateWindow(...);
ShowWindow(...); // probably not in your case
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
As you can see from the window setup boilerplate, this still relies on "silent windows," albeit created and message-pumped via the Win32 API rather than through WinForms. So you're not really gaining anything by doing this way. Hence my feeling there's not much point translating this stuff into C# -- if the only solution to your problem is an invisible window, you may as well use an invisible Windows Form and all the friendly wrappers that come with that platform.
However, if you're not actually using a Windows Forms control like the poster of the linked question, then you can quite happily use .NET events in a console application. The restriction to STA and the need for a message pump is specific to receiving events from WinForms and ActiveX controls like the WebBrowser (or messages from Win32 HWNDs, though that doesn't necessarily require STA).
这篇关于C# 控制台应用程序 + 事件处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!