Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8447109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:06:18+00:00 2026-06-10T10:06:18+00:00

//button is clicked //worker starts private void processWorker_DoWork(object sender, DoWorkEventArgs e) { string code

  • 0
//button is clicked
//worker starts

private void processWorker_DoWork(object sender, DoWorkEventArgs e)
{
     string code = DoLongWorkAndReturnCode();

     if (code != 0)
     {
         MessageBox.Show("Error!");
         EnableAllButtons(); // this is defined in the other thread and it's where i run into the error.
     }
     else
     {
          string code = DoAnotherLongProcessAndReturnCode(); 
          if (code != 0)
          {
               MessageBox.Show("Error 2!");
               EnableAllButtons(); // again, this is defined in the other thread
          }
     }
}

I’m running into a cross threading error because “EnableAllButtons()” is defined in a different thread.

How do I go about enabling all buttons in one thread, from a different thread?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T10:06:19+00:00Added an answer on June 10, 2026 at 10:06 am

    Based on your names I am assuming you are using a BackgroundWorker. You must either update your controls in ProgressChanged, RunWorkerCompleted, Control.Invoke or Control.BeginInvoke (You do not need to fire EndInvoke for Control’s BeginInvoke).

    My recommendation is throw a exception in your background worker (if you are not using code other than checking for 0, if not use the Result method.) and check the Error property of the parameter passed in to the RunWorkerCompleted event. From there you can spin up another background worker or stop. you will be running on the main thread so you can change your buttons without invoking.


    Example: Enabling the buttons via BeginInvoke

    With this example you can use your code as is just modify EnableAllButtons

    private void EnableAllButtons() 
    { 
        //See if the main form needs invoke required, assuming the buttons are on the same thread as the form
        if(this.InvokeRequired) 
        {
            //Calls EnableAllButtons a seccond time but this time on the main thread.
            //This does not block, it is "fire and forget"
            this.BeginInvoke(new Action(EnableAllButtons));
        }
        else
        {
            btnProcessImages.Enabled = true; 
            btnBrowse.Enabled = true; 
            btnUpload.Enabled = true; 
            btnExit.Enabled = true; 
            ControlBox = true;
        }
    }
    

    Example: Returning data via Result

    private void processWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
         string code = DoLongWorkAndReturnCode();
         e.Result = code;
    }
    private void processWorkers_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (code != 0)
        {
            MessageBox.Show("Error!");
            EnableAllButtons(); // this is defined in the other thread and it's where i run into the error.
        }
        else
        {
              doAnotherLongProcessAndReturnCodesBackgroundWorker.RunWorkerAsync(); 
        }
    }
    

    Example: Returning data via Exception + reusing event handlers.

    private void processWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
         string code = DoLongWorkAndReturnCode();
         if (code != 0)
         {
             thow new MyCustomExecption(code);
         }
    }
    
    private void processWorker2_DoWork(object sender, DoWorkEventArgs e)
    {
         string code = DoAnotherLongProcessAndReturnCode(); 
         if (code != 0)
         {
             thow new MyCustomExecption(code);
         }
    }
    
    private void processWorkers_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if(e.Error != null)
        {
            string message;
    
            //See if it was our error
            if(e.Error.GetType() == typeOf(MyCustomExecption))
            {
    
                //Choose which error message to send
                if(sender.Equals(processWorker2))
                    message = "Error2!";
                else
                    message = "Error!";
            }
            else
            {
                //Handle other types of thrown exceptions other than the ones we sent
                message = e.Error.ToString();
            }
    
    
            //no matter what, show a dialog box and enable all buttons.
            MessageBox.Show(message);
            EnableAllButtons();
        }
        else
        {
            //Worker completed successfully. 
            //If this was called from processWorker1 call processWorker2
    
            //Here is the code that was returned from the function
            if(sender.Equals(processWorker1))
                processWorker2.RunWorkerAsync();
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the following code every time button is clicked the alter shows the value
When a button is clicked, the following method is run: public void createTableRow(View v)
I would like to handle a button clicked event in a native c++ class.
Typical jQuery over-use: $('button').click(function() { alert('Button clicked: ' + $(this).attr('id')); }); Which can be
I have custom server control contains templatefield when an out button clicked the field
when a submit button is clicked i want to generate a pop up showing
After a button is clicked to start a process on my web app, I
When a button is clicked I check if something exists in a localstorage key
When 'login' button is clicked I would like to iterate through a column in
When a button is clicked, the end user is able to send an email.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.