2011-07-11 18 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

これをgoogleやstackoverflowで検索しようとしましたか? – vidstige

答えて

0

のみUIスレッドが直接UIコントロールにアクセスすることができますので、あなたは、backgroundWorker1.ReportProgress(1);を呼び出す必要があります。呼び出しの例:

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-スレッド以外 - スレッド以外) [リンク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が動作しました - ありがとうスポーツ - ミルクのポイントは何を意味するのか分かりませんが、混乱してごめんなさい。 他の人には - 申し訳ありません - スレッドは削除のためにフラグが立てられています。 IF(pbx_1.Location.X Gopher2011

関連する問題