在Windows应用程序中,当使用多个线程时,我知道有必要调用主线程来更新GUI组件.如何在控制台应用程序中完成?
例如,我有两个线程,一个主线程和一个辅助线程.辅助线程始终在侦听全局热键; 当它被按下时,辅助线程执行一个事件,该事件延伸到win32 api方法AnimateWindow.我收到一个错误,因为只允许主线程执行所述函数.
当"调用"不可用时,如何有效地告诉主线程执行该方法?
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 …Run Code Online (Sandbox Code Playgroud)