Link to home
Start Free TrialLog in
Avatar of Tech_Men
Tech_MenFlag for Israel

asked on

Cross-thread operation not valid using BackgroundWorker

hi there ,

i am on C# 2.0 using VS 2005
in the Form i have timer that need to run the backgroundWorker1.RunWorkerAsync();
every 100 seconds .

its look like that :

  private void timer1_Tick(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
        }
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            hb.GethatBakDet();
            listBox1.Items.Clear();
            if (hb.GetHatWorkers == true)
            {
                try
                {
                   
                    listBox1.Items.Clear();
                    List<string> list = new List<string>();

                    list = Notice.NoticeClass.GetWorkerNoitce();
                    if (list != null)
                    {
                        foreach (string l in list)
                            listBox1.Items.Add(l);

                    }

                }
                catch (Exception r)
                {
                    listBox1.Items.Add(r.Message);
                }

            if (hb.GetHatPotCust == true)
            {
                try
                {
                    List<string> list2 = new List<string>();
                    listBox3.Items.Clear();
                    list2 = Notice.NoticeClass.GetHatForPotCust();
                    if (list2 != null)
                    {
                        foreach (string t in list2)
                           listBox3.Items.Add(t);

                    }

                }
                catch (Exception r)
                {
                    listBox3.Items.Add(r.Message);
                }


            }
            else
            {
                listBox3.Items.Clear();
            }

           

        }

in debug when it reshed to this line :    listBox1.Items.Clear();

i have this Esx Message :   Cross-thread operation not valid: Control 'listBox1' accessed from a thread other than the thread it was created on.

how can i fix it ?
thanks ...
SOLUTION
Avatar of Priest04
Priest04
Flag of Serbia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is a simple code do get you started (I am typing of head):

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    OnDoWork();
}
 
void OnDoWork()
{
    if (this.InvokeRequired)
    {
        this.Invoke(new MethodInvoker(OnDoWork));
    }
    else
    {
        this.listBox1.Items.Clear();
        this.listBox1.Items.Add("Test");
    }
}

Open in new window

Avatar of Tech_Men

ASKER

plese help me Goran whit my code....
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks
You are welcome.