【问题标题】:C# error when a loop is inside a timer1_Tick function循环在 timer1_Tick 函数内时出现 C# 错误
【发布时间】:2017-08-24 10:26:36
【问题描述】:

我对线程了解不多。我只是在 UI 中有一个 timer1,当我将这个 while 循环放在 timer1_Tick 函数中时,它是 100 个间隔:

count = port.BytesToRead;
while (count > 0)
{
  // get the new byte:
  char inchar = (char)port.ReadChar();
  // add it to the inputstring:
  inputString += inchar;
  // if the incoming character is a newline, set a flag
  // so the main loop can do something about it:
  if (inchar == '\n')
  {
     stringComplete = true;
  }
}

程序停止响应任何 UI 输入。我知道问题出在线程和 UI 线程上,但正如我所说,我对线程的了解很少。
那么这样的问题有什么解决办法

【问题讨论】:

  • 你的定时器启用了吗?
  • @imsome1 是的
  • 它是否响应来自 UI 的第一个请求?

标签: c# multithreading timer


【解决方案1】:

您没有递减您的 count 变量,因此 while(count > 0) 永远不会退出。

【讨论】:

  • @sabsab 是的,但是您在循环之前设置值,并且从不更改循环内的值。因此,一旦将其设置为 > 0 的值,它将在循环中永远 > 0。
猜你喜欢
  • 2020-03-13
  • 2017-01-30
  • 1970-01-01
  • 1970-01-01
  • 2021-09-06
  • 2018-09-23
  • 1970-01-01
  • 2018-05-04
  • 2010-11-06
相关资源
最近更新 更多