2012-12-08 9 views
5

Ich schreibe ein Programm, das Befehle aus dem Serialport (von einem Arduino) überwacht und sendet Tastenanschläge an verschiedene andere Programme.Dispatcher in Ereignis wird nur einmal aufrufen C#

Ich habe einen Dispatcher in meinem Ereignis platziert, aber es wird nur einmal ausgeführt ... Ich habe durch den Code und es läuft die ganze Zeit durch das erste Mal, aber nicht das zweite Mal.

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

Werfen Sie einen Blick auf den Dispatcher-Thread bitte. – lontivero

Antwort

0

Ahh, Fixed ... Nicht sicher warum.

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

    } 
Verwandte Themen