Click here to Skip to main content
15,867,756 members
Home / Discussions / C#
   

C#

 
QuestionHow to fix Error when i using BackgroundWorker in C# Pin
Alviss_H20-Nov-12 20:36
Alviss_H20-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
C#
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
professionalAmitGajjar20-Nov-12 21:04 
GeneralRe: How to fix Error when i using BackgroundWorker in C# Pin
Alviss_H20-Nov-12 21:19
Alviss_H20-Nov-12 21:19 
GeneralRe: How to fix Error when i using BackgroundWorker in C# Pin
AmitGajjar20-Nov-12 21:22
professionalAmitGajjar20-Nov-12 21:22 
AnswerRe: How to fix Error when i using BackgroundWorker in C# Pin
Sivaraman Dhamodharan20-Nov-12 22:52
Sivaraman Dhamodharan20-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.