5
我正在寫一個程序來監視從串口(從一個arduino)發送的命令,並將擊鍵發送到各種其他程序。事件中的調度程序只會調用一次C#
我已經在我的事件中放置了一個調度程序,但它只會運行一次... 我已經遍歷代碼,它在第一次運行時一直運行,但不是第二次運行。
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.
}
}
請看調度器線程。 – lontivero