Click here to Skip to main content
15,913,090 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using backgroundworker control in my project, to show the progress bar, and I am getting cross thread exception in backgroundWorker_Dowok event.

Please help.

Here is my code:

C#
private void btnGenerate_Click(object sender, EventArgs e)
       {

           
           toolStripProgressBar1.Style = ProgressBarStyle.Marquee;
           backgroundWorker1.RunWorkerAsync();
       }
       private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
       {
                   
           bool flag;
            try
            {
             
               clientid = convert.ToString(ddlClientName.SelectedValue); 
               clientid = Convert.ToString(ddlprincipalname.SelectedValue); 
               clid = Convert.ToInt32(ddlClientName.SelectedValue);
               prid = Convert.ToInt32(ddlprincipalname.SelectedValue); 

                   flag = check_op_close_stock();
                   if (flag)
                   {
                       excel_header();
                       get_excel_data();
                       
                       get_nostk_transit();
                       set_formating();
                   }
               }
            
               catch (Exception ex)
               {
                   MessageBox.Show(ex.Message.ToString());
               }
           }
Posted
Updated 24-Nov-10 22:48pm
v2
Comments
Dalek Dave 25-Nov-10 4:48am    
Edited for Readability.
johannesnestler 25-Nov-10 9:49am    
What be a lot easier to help you if:
* You provide the code where the exception occurs (where you access your GUI-Components (Controls))
* Tell us, if you understand what the exception tells you!


I think you don't understand, so I recommend: Try to understand the problem - If you debug your code - click on help for that exception and follow the docu! If unsolvable by this learn how to use msdn docu and your programming language. Sorry for this rude answer, but im tired of explaining this issue 1000 times...

You have to make use of InvokeRequired property and the Invoke()method of a Control class.

Check this[^] article for the details/examples.
 
Share this answer
 
Comments
savita from Pune 25-Nov-10 4:07am    
I dont Know how to use this InvokeRequired property and method, can u send the code how to use this?
J a a n s 25-Nov-10 4:13am    
As a workaround, try to set the Control.CheckForIllegalCrossThreadCalls property as false.
savita from Pune 25-Nov-10 4:16am    
I used it in DoWork event before try block, but the error remains
savita from Pune 25-Nov-10 4:39am    
If anybody knows the answer, please reply fast, its an urgent
Dalek Dave 25-Nov-10 4:48am    
Good Answer.
There are tons of articles out there. Check out the official msdn documentation:

http://msdn.microsoft.com/en-us/library/ms171728(VS.80).aspx[^]

You did not post the method that updates the progress bar, but the invoke required must be checked in this method.

It should look something like this:
C#
private void setProgress(int nValue)
{
  if(progressBar.InvokeRequired)
    progressBar.Invoke(new my_delegate(setProgress),new object[]{nValue});
  else
  {    
    progressBar.Value = nValue;
  }
}

private delegate void my_delegate(int nValue);
 
Share this answer
 
v2
Comments
savita from Pune 25-Nov-10 5:50am    
Hi,when i click on Generate button, i want to show progress bar, till my data saved in excel sheet, that is till set_formating(); completed
but the error occurred at ddlclientname and ddlprincipalname(both are dropdownlist) that is Cross thread operation not valid
JF2015 25-Nov-10 8:04am    
So that makes it clear. Not only your progressbar is the problem, it's the dropdownlists too - of course they throw the same exception when you try to access them from a thread.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900