主窗体写法:
private delegate void CrossThreadOperationControl();
/// <summary>
/// 创建一个后台进程 进行跨线程操作控件
/// </summary>
public void BackgroundProcess()
{
// 将代理实例化为一个匿名代理
CrossThreadOperationControl CrossDelete = delegate ()
{
//调用filehandle类中的recivewebsocket方法 将label控件作为参数传进去
FileHandle.ReciveWebsocket(PromatMess);
};
PromatMess.Invoke(CrossDelete);
}
类中函数写法:
public static void ReciveWebsocket(Label label)
类中函数写法:
public static void ReciveWebsocket(Label label)
{
try
{
label.Text = "服务器已连接";
}
catch (Exception)
{
label.Text = "服务器已断开";
}
}
然后创建新线程
Thread thread = new Thread(BackgroundProcess);
然后创建新线程
Thread thread = new Thread(BackgroundProcess);
thread.IsBackground = true;
thread.Start();