简体   繁体   中英

C# error when a loop is inside a timer1_Tick function

I don't know much about threading. I simply have a timer1 in the UI, when I put this while loop inside the timer1_Tick function which is 100 intervals:

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;
  }
}

The program stop responding for any UI input.I know that the problem is about threading and UI thread but I have few knowledge in threading as I said.
So what is the solution for such a problem

您没有递减count变量,所以while(count > 0)永不退出。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM