在控制台应用程序的主线程上调用委托 [英] Invoke delegate on main thread in console application

查看:180
本文介绍了在控制台应用程序的主线程上调用委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows应用程序中,当使用多个线程时,我知道有必要调用主线程来更新GUI组件.在控制台应用程序中该如何完成?

In a Windows Application, when multiple threads are used, I know that it’s necessary to invoke the main thread to update GUI components. How is this done in a Console Application?

例如,我有两个线程,一个主线程和一个辅助线程.辅助线程始终在侦听全局热键.当按下时,辅助线程将执行一个事件,该事件将到达win32 api方法AnimateWindow.我收到一个错误,因为只允许主线程执行所述功能.

For example, I have two threads, a main and a secondary thread. The secondary thread is always listening for a global hotkey; when it is pressed the secondary thread executes an event that reaches out to the win32 api method AnimateWindow. I am receiving an error because only the main thread is allowed to execute said function.

当调用"不可用时,如何有效地告诉主线程执行该方法?

How can I effectively tell the main thread to execute that method, when "Invoke" is not available?


更新:(如果有帮助),下面是代码.要查看HotKeyManager的内容(正在使用其他线程),请查看


update: if it helps, here is the code. To see the HotKeyManager stuff(where the other thread is coming into play), check out the answer to this question

class Hud
{
    bool isHidden = false;
    int keyId;

    private static IntPtr windowHandle;

    public void Init(string[] args)
    {
        windowHandle = Process.GetCurrentProcess().MainWindowHandle;
        SetupHotkey();
        InitPowershell(args);
        Cleanup();
    }

    private void Cleanup()
    {
        HotKeyManager.UnregisterHotKey(keyId);
    }

    private void SetupHotkey()
    {

        keyId = HotKeyManager.RegisterHotKey(Keys.Oemtilde, KeyModifiers.Control);
        HotKeyManager.HotKeyPressed += new EventHandler<HotKeyEventArgs>(HotKeyManager_HotKeyPressed);
    }

    void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e)
    {
       ToggleWindow();
    }

    private void ToggleWindow()
    {
        //exception is thrown because a thread other than the one the console was created in is trying to call AnimateWindow

        if (isHidden)
        {
            if (!User32.AnimateWindow(windowHandle, 200, AnimateWindowFlags.AW_VER_NEGATIVE | AnimateWindowFlags.AW_SLIDE))
                throw new Win32Exception(Marshal.GetLastWin32Error());
        }
        else
        {
            if (!User32.AnimateWindow(windowHandle, 200, AnimateWindowFlags.AW_VER_POSITIVE | AnimateWindowFlags.AW_HIDE))
                throw new Win32Exception(Marshal.GetLastWin32Error());
        }
        isHidden = !isHidden;
    }

    private void InitPowershell(string[] args)
    {
        var config = RunspaceConfiguration.Create();
        ConsoleShell.Start(config, "", "", args);
    }
}

推荐答案

作为有关MSDN的文档说:

在以下情况下该功能将失败:

The function will fail in the following situations:

  • [...]
  • 如果线程不拥有窗口. [...]

因此,这里没有主"线程存在问题(AFAIK Win32Api不在乎您的程序入口点在其上执行的巫婆线程).

So there is no "main" thread in question here (AFAIK Win32Api doesn't care about witch thread your program entry point is executed on).

唯一的条件是,必须在拥有要设置动画的窗口的线程上执行AnimateWindow.那就是所谓的 CreateWindow ,因为它是定义线程/消息循环相关性的函数).

The only condition is that you must execute AnimateWindow on a thread that is owning the window that you are animating. That's the one that called CreateWindow as it is the function that define the thread / message-loop afinity).

  • 大多数时候,如乔恩所说,该线程应该运行由 Application.Run .
    从另一个线程,您可以使用 Control.Invoke 方法可强制主线程执行代码.如果没有Control引用,只需在主线程中创建一个引用,并将其命名为 PeekMessage 循环等待至少对于您定义的WM_QUIT和WM_EXECUTE_ANIMATE_WINDOW.通过PostMessage或PostThreadMessage构成此消息的辅助线程.
  • Most of the time as Jon said this thread should be running a message loop created by Application.Run.
    From another thread you could use the Control.Invoke method to force the main thread to execute code. If you don't have a Control reference, just create one while in the main thread and call it's CreateHandle method. If you have a main form just use it
  • The message loop could also be created the old school way, especially if you already create your window by PInvoke anyway. The main thread should have a standard PeekMessage loop waiting at least for WM_QUIT and a WM_EXECUTE_ANIMATE_WINDOW that you define. The secondary thread posing this message via PostMessage or PostThreadMessage.

现在您已经发布了示例代码,那么您将遇到的问题是,您不打算仅对任何窗口进行动画处理……而是要对控制台窗口本身进行动画处理……而您不希望这样做. t是它的所有者线程(否则,当您在应用程序中创建无限循环时,它不会刷新)...因此,除非您设法强制Windows在该线程上执行代码,否则将无法调用AnimateWindow.

Now that you have posted your sample code, the problem that you will have is that you aren't trying to animate just any window... but you are trying to animate the console window itself... And you aren't on it's owner thread (otherwise it won't refresh when you create an infinite loop in your application)... so calling AnimateWindow won't be possible except if you manage to coerce windows to execute code on that thread.

事实控制台窗口实际上归CSRSS所有,这是一个以提升的权限执行的系统进程,无论如何都会给他们带来混乱的风险.

The fact console windows are in fact owned by CSRSS witch is a system process executing with elevated rights make messing with them really risky anyway.

由于Windows Vista的存在,由于具有进程保护功能,因此甚至无法将消息发送到此类Windows,因此,以前可以用来胁迫该线程执行代码的任何漏洞现在都将无法使用.

Since windows vista it's even impossible to send a message to such windows due to process protection so any vulnerability that could be exploited previously to coerce this thread to execute code should now be unusable.

有关控制台窗口的特殊性的详细信息,请参见

For the details regarding the specificity of the console window see the Why aren't console windows themed on Windows XP? post on the Raymond Chen blog (from the microsoft windows shell team so it's pretty much from the source)

这篇关于在控制台应用程序的主线程上调用委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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