C# 控制台应用程序 + 事件处理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/764869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:15:41  来源:igfitidea点击:

C# Console App + Event Handling

c#event-handlingconsole-application

提问by bitcycle

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):

大家好。我正在尝试了解如何在控制台应用程序中处理事件。我宁愿不使用静默的 WinForms(尽管我明白这是一种方法)来做到这一点。我已经阅读了一个类似的问题及其回应。请参阅下面的回复文本(链接):

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.

STA 线程的基本要求是它需要运行消息泵。在 Windows 窗体中,您可以使用 Application.Run。或者您可以使用 user32!GetMessage 和 DispatchMessage 手动编写消息泵。但是在 WinForms 或 WPF 中使用它可能更容易。

What the basic structure of a program that uses "user32 -> GetMessage" & "user32 -> DispatchMessage"?

使用“user32 -> GetMessage”和“user32 -> DispatchMessage”的程序的基本结构是什么?

采纳答案by itowlson

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:

请参阅 MSDN 中的“使用消息和消息队列”主题(在 Win32 和 COM 开发下 > 用户界面 > Windows 用户体验 > Windows 管理 > Windows 用户界面 > 窗口 > 消息和消息队列;您可能需要查看同一部分中的其他文章和示例)。快速总结,出于以下讨论的原因,省略错误处理并使用 C 语法而不是 C#:

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.

正如您从窗口设置样板中看到的那样,这仍然依赖于“静默窗口”,尽管它是通过 Win32 API 而不是通过 WinForms 创建和消息泵送的。所以你这样做并没有真正获得任何东西。因此,我觉得将这些东西翻译成 C# 没有多大意义——如果您的问题的唯一解决方案是一个不可见的窗口,那么您不妨使用一个不可见的 Windows 窗体以及该平台附带的所有友好包装器。

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).

但是,如果您实际上没有像链接问题的海报那样使用 Windows 窗体控件,那么您可以很高兴地在控制台应用程序中使用 .NET 事件。对 STA 的限制和对消息泵的需求特定于从 WinForms 和 ActiveX 控件(如 WebBrowser)接收事件(或来自 Win32 HWND 的消息,尽管不一定需要 STA)。

回答by Paul Keister

What kind events do you want to handle? Setting up a message pump will allow you to process Windows API messages. But since this is a console app, I can't think of any Windows API messages that would be of interest.

你想处理什么样的事件?设置消息泵将允许您处理 Windows API 消息。但由于这是一个控制台应用程序,我想不出任何感兴趣的 Windows API 消息。

We tend to think of "Events" as being associated with Windows messages because controls in Windows Forms application respond to user input using EventHandler delegates, which are called in response to Windows API messages. However, there's no reason you can't use delegates in a console app; and you don't need a message pump. You can even use the EventHandler delegate, altough it will seem out of place because it won't be resonding to Windows API messages.

我们倾向于认为“事件”与 Windows 消息相关联,因为 Windows 窗体应用程序中的控件使用 EventHandler 委托响应用户输入,这些委托被调用以响应 Windows API 消息。但是,没有理由不能在控制台应用程序中使用委托;并且您不需要消息泵。您甚至可以使用 EventHandler 委托,尽管它看起来不合适,因为它不会响应 Windows API 消息。

Of course there are other kinds of "events" that you might be interested in. The most common kind of event scenario involving a console app is to wait for input from the console (stdin). It's also common to block on a WaitHandle or other synchronization object if there are multiple threads in use. Both of these cases could be considered a type of event handling.

当然,还有其他类型的“事件”您可能会感兴趣。涉及控制台应用程序的最常见的事件场景是等待来自控制台的输入 (stdin)。如果有多个线程在使用,则在 WaitHandle 或其他同步对象上阻塞也很常见。这两种情况都可以被视为一种事件处理。

If you create a hidden window (a time honored practice), you'll still need a Windows message to respond to. So the question is: to what, or to whom, are you trying to respond?

如果您创建了一个隐藏窗口(一种久经考验的做法),您仍然需要一条 Windows 消息来响应。所以问题是:你想对什么或对谁做出回应?

回答by Paul Keister

I would use the Application.Run method. I don't think it creates a hidden window automatically, it just pumps the messages, which is what you need to satisfy the STA requirement.

我会使用 Application.Run 方法。我认为它不会自动创建隐藏窗口,它只是泵送消息,这是满足 STA 要求所需要的。

Writing your own message pump using PInvoke doesn't offer any advantage and referencing System.Windows.Forms isn't much of a burden because it's already on every machine you may encounter.

使用 PInvoke 编写您自己的消息泵不会提供任何优势,并且引用 System.Windows.Forms 也不是什么负担,因为它已经存在于您可能遇到的每台机器上。