在C#中,因为使用线程和调用UI的线程属于两个不一样的线程,若是在线程中直接设置UI元素的属性,此时就会出现跨线程错误。this
ThreadPool.QueueUserWorkItem(ar => { this.button1.Invoke(new Action(() => { this.button1.Text = "aa"; })); });
private SynchronizationContext _syncContext = SynchronizationContext.Current; private void button1_Click(object sender, EventArgs e) { ThreadPool.QueueUserWorkItem(ar => { _syncContext.Post(p => { this.button1.Text = "aa"; }, null); }); }