1 回答

TA貢獻1921條經驗 獲得超9個贊
您正在處理程序中運行無限循環Afisare_Click
,該循環與您的 UI 在同一線程中運行。這意味著 UI 將無法呈現控件更改。
Thread.Sleep
代碼將上下文切換到其他線程,但不是 UI 線程。
您的方法應該是使用Timer。
public partial class Form1 : Form
{
? ? private Timer _timer;
? ? public Form1()
? ? {
? ? ? ? InitializeComponent();
? ? }
? ? private void Form1_Load(object sender, EventArgs e)
? ? {
? ? ? ? _timer = new Timer();
? ? ? ? _timer.Interval = 200;
? ? ? ? _timer.Tick += _timer_Tick;
? ? ? ? _timer.Enabled = true;
? ? ? ? _timer.Start();
? ? }
? ? private void _timer_Tick(object sender, EventArgs e)
? ? {
? ? ? ? // This function will be called every 200 ms.
? ? ? ? // Read the information from port, update the UI.
? ? ? ? string a = port.ReadExisting();
? ? ? ? afisare.Text = a;
? ? }
? ? private void Form1_FormClosed(object sender, FormClosedEventArgs e)
? ? {
? ? ? ? _timer.Stop();
? ? ? ? if (port != null && port.IsOpen)
? ? ? ? {
? ? ? ? ? ? port.Close();
? ? ? ? }
? ? }
}
- 1 回答
- 0 關注
- 143 瀏覽
添加回答
舉報