如何从线程打开新窗体
本文关键字:窗体 新窗体 线程 | 更新日期: 2023-09-27 18:26:04
我有一个异步套接字客户端,当数据包到达时,我需要打开一个新表单来处理它。
然而,由于它在线程中,新表单挂起并且没有响应。
如何从异步回调创建和打开新表单?
使用Dispatcher从后台切换到UI线程。
//This has to be done on the UI-Thread, before calling the async method
var dispatcher = Dispatcher.CurrentDispatcher;
//Now, in your async callback, do something like this
private void AsyncCallback(IAsyncResult result){
dispatcher.Invoke(new Action(() =>
{
//Create your form Here
}
}
如果您希望后台线程等待表单创建并显示,请使用Invoke(),否则,请使用Begin