简体   繁体   English

在阻止主线程的同时允许回调

[英]Allow callbacks while blocking the main thread

Given the following Code: 给出以下代码:

public static void main()
{
    NotifyWindow.NotifyIcon.DoubleClick += new EventHandler(NotifyIconDoubleClick);
    NotifyWindow.Show(500, "some text", "some title");
    Thread.Sleep(10000);
}

private static void NotifyIconDoubleClick(Object sender, EventArgs e)
{
    Clipboard.SetText("test");
}

(NotifyWindow being a wrapper of NotifyIcon ) (NotifyWindow是NotifyIcon的包装器)

I want to give the user the possibility to double click on the NotifyWindow so that the event is fired. 我想给用户双击NotifyWindow的可能性,以便触发该事件。 Sadly this doesn't work with the Thread.Sleep() method since the callback gets freezed for this time. 遗憾的是,这不适用于Thread.Sleep()方法,因为此时回调已冻结。

Any way to archieve this? 有什么办法可以存档吗?

You need a message loop for the event handling to work. 您需要一个消息循环才能使事件处理正常进行。 One way of achieving this is to create and "show" a hidden window, using ShowDialog or Application.Run() . 实现此目的的一种方法是使用ShowDialogApplication.Run()创建和“显示”隐藏的窗口。 If the action that should happen when you click the icon is showing a window, then it might be desirable to just create that window at the start already, but "show" it as hidden or minimized. 如果单击该图标时应执行的操作正在显示一个窗口,则可能希望仅在开始时创建该窗口,但将其“显示”为隐藏或最小化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM