C#跨线程操作无效

可能重复:
跨线程操作无效:从创建它的线程以外的线程访问控制。

我收到一条错误消息 – 任何人都可以给我一些指示。

跨线程操作无效:控制’pbx_1’从其创建的线程以外的线程访问。

我看过这里,但我似乎无法让它工作。 我对c#很新,所以我可能错过了一些东西。

Console.WriteLine("backgroundWorker1"); while (!backgroundWorker1.CancellationPending) { Thread.Sleep(100); if (pbx_1.Location.X  Click_X) { pbx_1.Location = new Point(20, pbx_1.Location.X - MoveAmt); } backgroundWorker1.ReportProgress(1); } 

你应该调用backgroundWorker1.ReportProgress(1); ,因为只有UI线程才能直接访问UI控件。 调用示例:

 private delegate void AddListBoxItemDelegate(object item); private void AddListBoxItem(object item) { if (this.listBox1.InvokeRequired) { // This is a worker thread so delegate the task. this.listBox1.Invoke(new AddListBoxItemDelegate(this.AddListBoxItem), item); } else { // This is the UI thread so perform the task. this.listBox1.Items.Add(item); } }