几本资料C#并发编程经典实例
例子1:子线程更新UI
public void DoThreading() { ThreadStart starter = new ThreadStart(UpdateListBox); Thread t = new Thread(starter); t.Start(); for (int i = 0; i < 4; i++) { listBox.Items.Add(new SkinListBoxItem("Message from UI")); listBox.Update(); } } public string Message = ""; public void WorkerUpdate(object sender, EventArgs e) { listBox.Items.Add(new SkinListBoxItem(Message)); listBox.Update(); } public void UpdateListBox() { for (int j = 0; j < 5; j++) { this.Message = "辅助线程的循环数" + j.ToString(); this.listBox.Invoke(new EventHandler(WorkerUpdate)); Thread.Sleep(4700); } }
时间: 2024-11-06 18:12:47