• <i id='xDjgr'><tr id='xDjgr'><dt id='xDjgr'><q id='xDjgr'><span id='xDjgr'><b id='xDjgr'><form id='xDjgr'><ins id='xDjgr'></ins><ul id='xDjgr'></ul><sub id='xDjgr'></sub></form><legend id='xDjgr'></legend><bdo id='xDjgr'><pre id='xDjgr'><center id='xDjgr'></center></pre></bdo></b><th id='xDjgr'></th></span></q></dt></tr></i><div id='xDjgr'><tfoot id='xDjgr'></tfoot><dl id='xDjgr'><fieldset id='xDjgr'></fieldset></dl></div>

      <small id='xDjgr'></small><noframes id='xDjgr'>

      <legend id='xDjgr'><style id='xDjgr'><dir id='xDjgr'><q id='xDjgr'></q></dir></style></legend>

        • <bdo id='xDjgr'></bdo><ul id='xDjgr'></ul>
      1. <tfoot id='xDjgr'></tfoot>

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

        时间:2023-09-15

          <bdo id='ZY1WD'></bdo><ul id='ZY1WD'></ul>
          • <i id='ZY1WD'><tr id='ZY1WD'><dt id='ZY1WD'><q id='ZY1WD'><span id='ZY1WD'><b id='ZY1WD'><form id='ZY1WD'><ins id='ZY1WD'></ins><ul id='ZY1WD'></ul><sub id='ZY1WD'></sub></form><legend id='ZY1WD'></legend><bdo id='ZY1WD'><pre id='ZY1WD'><center id='ZY1WD'></center></pre></bdo></b><th id='ZY1WD'></th></span></q></dt></tr></i><div id='ZY1WD'><tfoot id='ZY1WD'></tfoot><dl id='ZY1WD'><fieldset id='ZY1WD'></fieldset></dl></div>

                <tbody id='ZY1WD'></tbody>
            1. <tfoot id='ZY1WD'></tfoot>

              <small id='ZY1WD'></small><noframes id='ZY1WD'>

                1. <legend id='ZY1WD'><style id='ZY1WD'><dir id='ZY1WD'><q id='ZY1WD'></q></dir></style></legend>

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

                  问题描述

                  大家好.我正在尝试了解如何在控制台应用程序中处理事件.我宁愿不使用静默 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# 控制台应用程序 + 事件处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用“Control.CheckForIllegalCrossThreadCalls = false"是个好主 下一篇:C# 取消 Windows 关机

                  相关文章

                    <legend id='BCPtZ'><style id='BCPtZ'><dir id='BCPtZ'><q id='BCPtZ'></q></dir></style></legend>
                  1. <i id='BCPtZ'><tr id='BCPtZ'><dt id='BCPtZ'><q id='BCPtZ'><span id='BCPtZ'><b id='BCPtZ'><form id='BCPtZ'><ins id='BCPtZ'></ins><ul id='BCPtZ'></ul><sub id='BCPtZ'></sub></form><legend id='BCPtZ'></legend><bdo id='BCPtZ'><pre id='BCPtZ'><center id='BCPtZ'></center></pre></bdo></b><th id='BCPtZ'></th></span></q></dt></tr></i><div id='BCPtZ'><tfoot id='BCPtZ'></tfoot><dl id='BCPtZ'><fieldset id='BCPtZ'></fieldset></dl></div>

                    • <bdo id='BCPtZ'></bdo><ul id='BCPtZ'></ul>

                  2. <tfoot id='BCPtZ'></tfoot>

                    1. <small id='BCPtZ'></small><noframes id='BCPtZ'>