我想用SerialPort + BeginInvoke实现 当SerialPort收到数据时,先调用AnalyzeAndRespond方法 分析数据包数据 并作出相应的反应,然后再调用UpdateTextBox2方法将收到的数据显示到TextBox控件中,我这里该怎么做呀?怎么总是窗口死掉呀?我考虑是多线程的问题,但是我很菜,不知道该怎么弄了,请各位高手指点

解决方案 »

  1.   

    怎么总是窗口死掉呀
    是应为发生了死锁~
    再好好去检查一下!线程的编写!
    既然是线程的问题:
    那你去学习一下线程:
    http://www.vchome.net/dotnet/dotnetdocs/dotnet1.htm
      

  2.   


    delegate void HandleInterfaceUpdateDelegate(string text); //委托 
     delegate void HandleInterfaceUpdateDelegate2(byte[] text);//byte [] text
     HandleInterfaceUpdateDelegate interfaceUpdateHandle2;
     HandleInterfaceUpdateDelegate2 interfaceUpdateHandle3;
     interfaceUpdateHandle2 = new HandleInterfaceUpdateDelegate(UpdateTextBox2);
    spcom2.DataReceived += new SerialDataReceivedEventHandler(spcom2_DataReceived);
    if (spcom2.BytesToRead > 0) spcom2.ReceivedBytesThreshold = spcom2.BytesToRead;
     else spcom2.ReceivedBytesThreshold = 17;      
    interfaceUpdateHandle3 = new HandleInterfaceUpdateDelegate2(AnalyzeAndRespond);
    spcom.DataReceived += new SerialDataReceivedEventHandler(spcom2_DataReceived);
     spcom.ReceivedBytesThreshold = 17;//事件发生前内部输入缓冲区中的字节数
     public void spcom2_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                int nSize=0;           
                try
                {
                    byte[] readBuffer = new byte[spcom2.ReadBufferSize];
                    nSize=spcom2.Read(readBuffer, 0, readBuffer.Length);
                    asyncResult = this.BeginInvoke(interfaceUpdateHandle3, readBuffer);
                    string BufferToOut = ByteArrayToHexString(readBuffer, nSize);
                   asyncResult2 = this.BeginInvoke(interfaceUpdateHandle2, BufferToOut);
                    spcom2.DiscardInBuffer();            }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message +"系统提示6");
                }
               
            }
    private void AnalyzeAndRespond(byte [] readBuffer)//数据包分析并相应
    {
    }
     private void UpdateTextBox2(string text) //更新窗口显示信息
    {}请帮忙分析,指点一下,谢谢
      

  3.   


    delegate void HandleInterfaceUpdateDelegate(string text); //委托 
     delegate void HandleInterfaceUpdateDelegate2(byte[] text);//byte [] text
     HandleInterfaceUpdateDelegate interfaceUpdateHandle2;
     HandleInterfaceUpdateDelegate2 interfaceUpdateHandle3;
     interfaceUpdateHandle2 = new HandleInterfaceUpdateDelegate(UpdateTextBox2);
    spcom2.DataReceived += new SerialDataReceivedEventHandler(spcom2_DataReceived);
    if (spcom2.BytesToRead > 0) spcom2.ReceivedBytesThreshold = spcom2.BytesToRead;
     else spcom2.ReceivedBytesThreshold = 17;      
    interfaceUpdateHandle3 = new HandleInterfaceUpdateDelegate2(AnalyzeAndRespond);
    spcom.DataReceived += new SerialDataReceivedEventHandler(spcom2_DataReceived);
     spcom.ReceivedBytesThreshold = 17;//事件发生前内部输入缓冲区中的字节数
     public void spcom2_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                int nSize=0;           
                try
                {
                    byte[] readBuffer = new byte[spcom2.ReadBufferSize];
                    nSize=spcom2.Read(readBuffer, 0, readBuffer.Length);
                    asyncResult = this.BeginInvoke(interfaceUpdateHandle3, readBuffer);
                    string BufferToOut = ByteArrayToHexString(readBuffer, nSize);
                   asyncResult2 = this.BeginInvoke(interfaceUpdateHandle2, BufferToOut);
                    spcom2.DiscardInBuffer();            }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message +"系统提示6");
                }
               
            }
    private void AnalyzeAndRespond(byte [] readBuffer)//数据包分析并相应
    {
    }
     private void UpdateTextBox2(string text) //更新窗口显示信息
    {}请帮忙分析,指点一下,谢谢