Click here to Skip to main content
12,297,439 members (61,946 online)
   

C#

 
QuestionHow to fix Error when i using BackgroundWorker in C# Pin
sonthuy_st20-Nov-12 20:36
membersonthuy_st20-Nov-12 20:36 
this's the error when I run the program "Cross-thread operation not valid: Control 'listBox1' accessed from a thread other than the thread it was created on." WTF | :WTF:
please help me Dead | X|
this's my code, it's simple
private void button1_Click(object sender, EventArgs e)
       {
           backgroundWorker2.RunWorkerAsync();
       }
       public void additem(int i)
       {
           listBox1.Items.Add(i);
       }
 
       private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
       {
           for (int i = 0; i < 10; i++)
           {
               additem(i);
               Thread.Sleep(500);
           }
 
       }

AnswerRe: How to fix Error when i using BackgroundWorker in C# Pin
@AmitGajjar20-Nov-12 21:04
member@AmitGajjar20-Nov-12 21:04 
GeneralRe: How to fix Error when i using BackgroundWorker in C# Pin
sonthuy_st20-Nov-12 21:19
membersonthuy_st20-Nov-12 21:19 
GeneralRe: How to fix Error when i using BackgroundWorker in C# Pin
@AmitGajjar20-Nov-12 21:22
member@AmitGajjar20-Nov-12 21:22 
AnswerRe: How to fix Error when i using BackgroundWorker in C# Pin
sirama200420-Nov-12 22:52
membersirama200420-Nov-12 22:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web01 | 2.8.160525.2 | Last Updated 27 May 2016
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid