C# 中关于子线程调用父线程的控件 Cross-thread operation not valid 错误

先说明下我的初衷,因为调用第三方的库,需要在我们这边实例化一个控件,传给第三方库去
最后在主线程里面启动子线程
问题:

子线程结束后如何自动通知主线程?

解决:

利用委托和回调。
private Thread linkthread;
//委托
public delegate void SendToParent();
private void ConnServer()
{
//线程的相关操作
SendToParent send1 = new SendToParent(ConnServerRes);
this.BeginInvoke(send1); //必须是控件实例化后调用才生效,所以需要窗口初始化后再隐藏
}
private void ConnServerRes()
{
ButelTryEnableCamera(true);
ButelSetVideoWindow((Int32)PictureBoxtoIntPtr(m_localpictureBox), (Int32)PictureBoxtoIntPtr(pictureBox1)); //将父窗口控件句柄赋值给子线程异步执行
//初始化音频和视频窗口
var keystr = common.Common.GetValue(“userconfig”, “key”);
var username = common.Common.GetValue(“userconfig”, “username”);
var userpw = common.Common.GetValue(“userconfig”, “userpw”);
var alias = common.Common.GetValue(“userconfig”, “alias”);
//初始化坐席
ButelTryLogin(keystr, username, userpw, alias);
}

猜你喜欢

转载自blog.csdn.net/u012453032/article/details/80311034