sabsab sabsab - 6 months ago 33
C# Question

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

Answer Source

You are not decrementing your count variable so while(count > 0) never exits.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download