2017-08-24 86 views
2

我對線程知之甚少。我只是有在UI Timer1的,當我把這個while循環的timer1_Tick函數,它是100個區間內:當一個循環在timer1_Tick函數內時出現C#錯誤

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 input.I知道問題是關於線程和UI線程,但我沒有什麼線程知識,正如我所說的。
那麼這樣的問題的解決方案是什麼

+0

您的計時器是否啓用? – imsome1

+0

@ imsome1是的這是 – sabsab

+0

@Dmitry是啊,這就是我現在正在做的事情:P – sabsab

回答

3

你不會遞減你的count變量,所以while(count > 0)永遠不會退出。

+0

計數是我從串口讀取的字節數 – sabsab

+0

@sabsab是的,但是您在循環之前設置該值,並且永遠不會更改循環內的值。因此,一旦它被設置爲> 0的值,它將永遠在循環中> 0。 –

+2

感謝人,它就像你的回答一樣簡單:) – sabsab

相關問題