Click here to Skip to main content
12,819,727 members (39,536 online)
   

C#

 
QuestionHow to fix Error when i using BackgroundWorker in C# Pin
sonthuy_st20-Nov-12 21:36
membersonthuy_st20-Nov-12 21: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 22:04
member@AmitGajjar20-Nov-12 22:04 
GeneralRe: How to fix Error when i using BackgroundWorker in C# Pin
sonthuy_st20-Nov-12 22:19
membersonthuy_st20-Nov-12 22:19 
GeneralRe: How to fix Error when i using BackgroundWorker in C# Pin
@AmitGajjar20-Nov-12 22:22
member@AmitGajjar20-Nov-12 22:22 
AnswerRe: How to fix Error when i using BackgroundWorker in C# Pin
sirama200420-Nov-12 23:52
membersirama200420-Nov-12 23: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
Web02 | 2.8.170308.1 | Last Updated 25 Mar 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid