c#中多執行緒重新整理UI
阿新 • • 發佈:2019-02-14
建立後臺執行緒重新整理UI:
//建立代理 private delegate void DelegateRefreshUI(); //真正執行重新整理UI的函式 private void freshUI() { //重新整理UI } //重新整理UI函式 private void RefreshUI() { //每隔5秒重新整理UI while(true) { Thread.Sleep(5000); Dispatcher.Invoke(DispatcherPriority.Normal,new DelegateRefreshUI(freshUI)); } } //建立後臺重新整理UI執行緒 Thread t = new Thread(new ThreadStart(RefreshUI)); t.IsBackground = true; t.Start();
多執行緒中實時重新整理UI:
//建立代理 public delegate void delegateSetLabelTxt(string strTxt); public delegateSetLabelTxt setLabelTxt; //設定Label的text private void SetTxt(string strTxt) { if(this.label1.InvokeRequired) { this.setLabelTxt += new delegateSetLabelTxt(SetTxt); this.Invoke(setLabelTxt, new object[] { strTxt }); } else { this.label1.Text = strTxt; } }