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

Neo*_*n_c 5 c# events action dispatcher

我正在编写一个程序来监视从串口发送的命令(来自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.
                }
            }
Run Code Online (Sandbox Code Playgroud)

Neo*_*n_c 0

啊,已修复...不知道为什么。

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

    }
Run Code Online (Sandbox Code Playgroud)