2012-12-08 14 views
5

Tôi đang viết chương trình giám sát các lệnh được gửi từ cổng nối tiếp (từ arduino) và gửi tổ hợp phím đến các chương trình khác.điều phối viên trong sự kiện sẽ chỉ gọi một lần C#

Tôi đã đặt một điều phối viên trong sự kiện của mình, nhưng nó sẽ chỉ chạy một lần ... Tôi đã bước qua mã và chạy hết lần đầu tiên nhưng không phải lần thứ hai.

private void portReadEvent(object sender, SerialDataReceivedEventArgs args) 
     { //break here 
      Action dump = delegate() 
      { 
       dumpInfoText(serialPort.ReadExisting()); 
      }; //create my deligate 

      txt_portInfo.Dispatcher.Invoke(dump); //run dispatcher 

     } 

     private void dumpInfoText(string text) 
     { 
      string completeCommand = ""; 
      foreach (char C in text) 
      { 
       if (serDump.Length <=8 && (C != (char)0x0A || C != (char)0x0D)) //check 
       { 
        serDump = serDump + C; //add char 
       } 
       if (serDump.Length == 8) //check to see if my info has a complete command (serDump is global) 
       { 
        completeCommand = serDump; //move to complete 
        serDump = ""; // reset dump 
        if (C == (char)0x0A || C == (char)0x0D) //dont add crlf 
        { 
         serDump = serDump + C; //add char 
        } 
       } 
       if (completeCommand.Length == 8) 
       { 
        txt_portInfo.Text = txt_portInfo.Text + completeCommand; //do something with it. 
       } 
      } 
+3

hãy xem chủ đề của bộ điều phối. – lontivero

Trả lời

0

Ahh, Cố định ... Không chắc chắn lý do.

private void portReadEvent(object sender, SerialDataReceivedEventArgs args) 
    { 
     txt_portInfo.Dispatcher.Invoke(new Action(() => {dumpInfoText(serialPort.ReadExisting());})); //run dispatcher 

    } 
Các vấn đề liên quan