private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
  {
  System.Threading.Thread.Sleep(100);
  int bytes = serialPort1.BytesToRead;
  byte[] buffer = new byte[bytes];
  if (bytes == 0)
  { return; }
  serialPort1.Read(buffer, 0, bytes);
  string s100 = ByteArrayToHexString(buffer);//字节数组转为十六进制字符串
  TextBox1.Text += s100 + "\r\n";   
  }
当串口接到数据的时候提示: 未处理的“system.InvalidoperationException”类型的异常出现在 System.windowsforms.dll中。
  其他信息:线程间操作无效:从不是创建控件“TextBox1”的线程访问它。昨天发的求教帖子,后来发现有一位前辈回答:TextBox1.Text += s100 + "\r\n";
这里,invoke下我现在想问的是 这里具体怎么用invoke,感觉这样比我昨天理解的另一个答案要更简洁一些,请不吝赐教

解决方案 »

  1.   

      TextBox1.Text += s100 + "\r\n"; 替换为AddTextBox(s100 + "\r\n"); 方法定义如下:
     delegate void SetCallBack(string str);
            private void AddTextBox(string str)
            {
                if (TextBox1.InvokeRequired)
                {
                    SetCallBack d = new SetCallBack(AddList);
                    this.Invoke(d, new object[] { str });
                }
                else
                {
                    TextBox1.Text += str;
                }
            }
      

  2.   

    private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
    //这句写的很好,避免下面直接用属性,属性随时改变。
    //但变量名最好用表示长度的,bytes看上去是数组
    int n = serialPort1.BytesToRead;
    if(n==0) return;
    byte[] buffer = new byte[n];
    serialPort1.Read(buffer, 0, n);//读所有数据
    string s100 = ByteArrayToHexString(buffer);//字节数组转为十六进制字符串
    this.Invoke(delegate(object s, EventArgs e)
    {
    TextBox1.Text += s100 + "\r\n";
    });
    }
      

  3.   


    运行时错误 CS1660:无法将匿名方法转换为类型“System.delegate”,因为他不是委托类型
               CS0316:不能在此范围内生命为“e”的局部变量,因为这样会使“e”具有不同的含义,而它已在“父级或当前”范围中表示其他内容了 这是什么意思呀·?由于我能上网的这个机器装不了 VS08 所以我是照着屏幕敲的代码 所以有些慢 呵呵
      

  4.   

    少写了个类型转换,重贴。private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        //这句写的很好,避免下面直接用属性,属性随时改变。
        //但变量名最好用表示长度的,bytes看上去是数组
        int n = serialPort1.BytesToRead;
        if(n==0) return;
        byte[] buffer = new byte[n];    
        serialPort1.Read(buffer, 0, n);//读所有数据
        string s100 = ByteArrayToHexString(buffer);//字节数组转为十六进制字符串
        this.Invoke((EventHandler)delegate(object s, EventArgs e)
        {
            TextBox1.Text += s100 + "\r\n";
        });
    }
      

  5.   

    再简化一次,让代码更优雅一些
    private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        //这句写的很好,避免下面直接用属性,属性随时改变。
        //但变量名最好用表示长度的,bytes看上去是数组
        int n = serialPort1.BytesToRead;
        if(n==0) return;
        byte[] buffer = new byte[n];    
        serialPort1.Read(buffer, 0, n);//读所有数据
        string s100 = ByteArrayToHexString(buffer);//字节数组转为十六进制字符串
        this.Invoke((EventHandler)delegate { TextBox1.Text += s100 + "\r\n"; });
    }
      

  6.   


    稍微更正一点点,很多书上叫这个为匿名委托。其实这不是,只是匿名方法。匿名委托只有vb.net支持。c#只支持匿名方法。
    匿名委托是什么意思呢?就是说可以给一段代码在函数内定义一个名字,然后用这个函数做变量继续操作,这是函数式编程的方法,vb.net已经支持了。c#不支持,c#的只是匿名方法而已。
      

  7.   

    不过我觉得这样的可读性:    this.Invoke((EventHandler)delegate(object s, EventArgs e)
        {
            TextBox1.Text += s100 + "\r\n";
        });
    比起这个好一些:
     this.Invoke((EventHandler)delegate { TextBox1.Text += s100 + "\r\n"; });
      

  8.   


    但是我调试发现前者有错误就像我方才贴的那个,而后者已经调试成功了 ,非常感谢wuyazhe与computerfox的讨论与支持,同时也谢谢cyhf00808 的回帖,感激不尽 呵呵