首页 > 代码库 > C# 跨线程调用form控件技巧及byte[]与string型相互转换

C# 跨线程调用form控件技巧及byte[]与string型相互转换

 跨线程调用form控件技巧

private delegate void MethodSocket(object obj);//使用托管
ss = "OK";       
 this.BeginInvoke(new MethodSocket(InvokerReadMsg), ss);//this指向本窗体,回调函数InvokerReadMsg,

        private void InvokerReadMsg(object obj)//在这个函数里面可以直接访问Form控件<span style="font-family: Arial, Helvetica, sans-serif;">txtContent</span>

        {
            this.txtContent.Text += obj.ToString() + "\r\n";
        }


byte[]与string型相互转换

//byte 转string
string test = Encoding.Default.GetString(bytes, 0, length);
//string 转 byte
 byte[] bytes = System.Text.Encoding.Default.GetBytes(test);