【问题标题】:dispatcher in event will only invoke once C#事件中的调度程序只会调用一次 C#
【发布时间】:2012-11-26 06:53:33
【问题描述】:

我正在编写一个程序来监视从串行端口(从 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.
                }
            }

【问题讨论】:

  • 请查看调度程序线程。

标签: c# events action dispatcher


【解决方案1】:

啊,解决了……不知道为什么。

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

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 1970-01-01
    • 2016-05-07
    相关资源
    最近更新 更多