gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 23:51:57 25 4
gpt4 key购买 nike

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

最佳答案

啊,固定...不知道为什么。

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

}

关于c# - 事件中的调度程序只会调用一次 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13774197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com