C#控制台应用程序+事件处理 [英] C# Console App + Event Handling

查看:224
本文介绍了C#控制台应用程序+事件处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有。我想看到在控制台应用程序处理事件。我想preFER不使用无声的WinForms(虽然我明白,这是一种方式)来做到这一点。我读了一个类似的问题,它的响应。见下面响应文本(<一个href=\"http://stackoverflow.com/questions/687146/webbrowser-control-console-application-events-not-firing/687165#687165\">link):

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

STA线程的基本要求
  是,它需要运行的消息
  泵。在Windows窗体中,可以使用
  Application.Run。或者,你可以写
  用手消息泵,使用
  !USER32的GetMessage&安培; DispatchMessage函数。
  但是,它可能更容易使用
  之一的WinForms或WPF。

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.

什么,它使用USER32 - >的GetMessage程序的基本结构&放大器; ?USER32 - >在DispatchMessage

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

推荐答案

请参阅MSDN中的主题使用邮件和消息队列(在Win32和COM开发>用户界面> Windows用户体验> Windows管理> Windows用户界面>窗口>消息和消息队列,你可能需要看看在同一节中的其他文章和样本)。简单总结,省略错误处理和使用C语法,而不是C#为下述原因:

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);
}

你可以从窗口设置样板看,这仍依赖于沉默的窗户,虽然创造,并通过在Win32 API,而不是通过的WinForms消息抽水。所以,你没有真正获得通过做这样事情。因此,我的感觉没有太多的点翻译这个东西到C# - 如果你的问题的唯一解决方案是一个不可见的窗口,你不妨用一种无形的Windows窗体和所有友好的包装来与该平台

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.

不过,如果你没有实际使用Windows窗体控件类的链接问题的海报,那么你可以很愉快地使用.NET事件在控制台应用程序。向STA的限制,并需要一个消息泵是特定于接收来自WinForms和ActiveX控件像web浏览器事件(从Win32的HWNDs或消息,尽管这并不一定需要STA)的

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#控制台应用程序+事件处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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