事件中的调度程序只会被C#调用一次

5
我正在编写一个程序,监控从串口(来自Arduino)发送的命令,并向其他各种程序发送按键。我在事件中放置了一个调度程序,但它只会运行一次...我已经逐步执行了代码,第一次完全运行,但第二次没有运行。
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
请查看调度线程。 - lontivero
1个回答

0

啊,修好了...不确定为什么。

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

    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接