2011-07-11 97 views
-4

可能重複:
Cross-thread operation not valid: Control accessed from a thread other than the thread it was created on.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); 
    } 

    if (pbx_1.Location.X > Click_X) { 
     pbx_1.Location = new Point(20, pbx_1.Location.X - MoveAmt); 
    } 

    backgroundWorker1.ReportProgress(1); 
} 
+5

您是否嘗試過在谷歌和/或計算器上搜索此內容? – vidstige

回答

0

您應該調用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); 
    } 
} 
+3

擠奶點或鏈接重複...我看到你去這裏的方式[鏈接1](http://stackoverflow.com/questions/1353857/cross-thread-operation-not-valid-control-accessed-from-一個線程其他超所述-T); [鏈接2](http://stackoverflow.com/questions/142003/cross-thread-operation-not-valid-control-accessed-from-a-thread-other-than-the-t); [鏈接3](http://stackoverflow.com/questions/244591/why-am-i-getting-this-errorcross-thread-operation-not-valid-control-lbfolders); [Link4](http://stackoverflow.com/questions/2237722/cross-thread-operation-not-valid-control-accessed-from-a-thread-other-than-the-t) – Smudge202

+0

鏈接1工作 - 謝謝運動 - 不確定牛奶的觀點意味着什麼,但對於任何混淆抱歉。 對其他人 - 對不起 - 線程標記爲刪除 - 道歉。 如果(pbx_1.Location.X Gopher2011