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

  • Home
  • SEARCH
  • 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 3664030
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:35:53+00:00 2026-05-19T01:35:53+00:00

I inherited some code that has two non-UI threads that update various WinForm controls.

  • 0

I inherited some code that has two non-UI threads that update various WinForm controls.
The code is using InvokeRequired and Invoke to update the UI; however, I still once in a while get the error: Cross-thread operation not valid: Control ‘lvReports’ accessed on a thread other than it was created on.

I suspect I am dealing with a race condition and that I need to introduce a lock into the method below, but that said, I can find dozens of examples on how to update UI from a non-UI thread safely but no examples or discussion on how to deal with two threads updating the same controls in a race scenario.

So my question is: how do I rewrite the code below to handle updating the UI properly given a race condition and that I need to update UI from non-UI threads?

// two separate theads call this method in a instance of a WinForm
private void LoadReports()
{
  if (this.InvokeRequired)
  {
    this.Invoke(new MethodInvoker(this.LoadReports));
  }
  else
  {
    // some code removed to keep exampe simple...
    SetCtlVisible(lvReports, true);

    if (this.InvokeRequired)
    {
      this.Invoke((MethodInvoker)delegate { lvReports.Refresh(); });
    }
    else
    {
       lvReports.Refresh();
    }
  }
}

delegate void SetVisibleCallback(Control ctl, bool visible);
private void SetCtlVisible(Control ctl, bool visible)
{
  if (ctl.InvokeRequired)
  {
    SetVisibleCallback d = new SetVisibleCallback(SetCtlVisible);
    ctl.Invoke(d, new object[] { ctl, visible });
  }
  else
  {
    ctl.Visible = visible;
  }
}

Here are some thoughts:
Does this.InvokeRequired differ from ctl.InvokeRequired at any time?
Is the second InvokeRequired test needed given the first?
Is the implementation of SetCtlVisible needed if I keep the first InvokeRequired?
Should I delete the first InvokeRequired and keep all the code in the else clause?
Is lock needed around the else clause?

  • 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-05-19T01:35:53+00:00Added an answer on May 19, 2026 at 1:35 am

    Using InvokeRequired like this is an anti-pattern. You know that this method is getting called from a thread, InvokeRequired should always be true.

    Now you can use it to troubleshoot your problem. If it is false then there’s something seriously wrong. Throw an exception, the debugger will stop and let you find out why it isn’t working properly. And always call Invoke(), invoke to a little helper method that does the rest of LoadReports().

    Also note that you are using it wrong in the rest of your code. You know that the remainder of LoadReports() runs on the UI thread, you used Invoke(). No point in testing it again, including inside SetCtlVisible().

    The typical reason for getting the bomb is because the thread is running LoadReports() too soon, before the form’s window is created. You need to interlock that. The form’s Load event is the signal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm seeing some code I've inherited that looks like the following: @interface SomeClass (private)
I have some C# / asp.net code I inherited which has a textbox which
I have inherited a middle tier system with some multi-Threading issues. Two different threads,
I have some code that I've inherited, and it's not the greatest in the
Good morning, I inherited some legacy code at work and it is using a
I have inherited some legacy PHP code what was written back when it was
In some code I've inherited, I see frequent use of size_t with the std
I'm refactoring some code I inherited from a long-gone developer, and I find this:
I came across some Enumerators that inherited uint . I couldn't figure out why
I've inherited a rather large application that really could use some cleanup. There is

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.