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 8416997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:55:21+00:00 2026-06-10T01:55:21+00:00

We are building a .NET application using WinForms (3.5). I have added a new

  • 0

We are building a .NET application using WinForms (3.5).

I have added a new feature recently, and started experiencing weird behavior when accessing certain controls. The problem was that some UI control access simply halted the execution (no exception was seen).

Under close examination (using WinDbg) I realized the controls were being updated from a ThreadPool thread, and a CrossThreadMessagingException was thrown.

My question is — is there any good practice on how to circumvent such behavior?

It would be very cumbersome but also perhaps not possible to surround every code location that accesses UI controls with the Control.Invoke method.

How can i partition my code to “safe” code that shouldn’t use Invoke, from one that should?

  • 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-10T01:55:23+00:00Added an answer on June 10, 2026 at 1:55 am

    If the application is designed to be multithreaded, then cross-threading can occur and thus you need to check for it using InvokeRequired, and either have the method you are calling re-Invoke() itself on the UI thread, or throw an exception that will indicate code is being used improperly. Keep in mind that InvokeRequired will be false in certain circumstances (mainly when the window doesn’t have a handle or is being/has been disposed); the best way to prevent these circumstances is to not start threads earlier in the window initialization process than the Load() event handler, and to handle the Closing() event by cancelling background threads created by the window and waiting for them to close.

    If the application is not multithreaded (you are not setting up BackgroundWorkers, TPL operations, BeginInvoke()ing delegates or Start()ing threads), then it’s not necessary. However, calls to InvokeRequired are pretty cheap (the logic behind it is basically a check that the WinAPI functions GetThreadId and GetWindowThreadProcessId return the same value), so if you anticipate the program being restructured to be multithreaded, the following patterns for called methods are simple enough to implement:

    //no return value, no parameters; ShowWindow(), HideWindow(), etc
    //Understand that many built-in control methods are not virtual and so you can't 
    //override them to do this; you must either hide them or ensure the caller is
    //checking for cross-threading.
    public void MyWindowMethod()
    {
       if(InvokeRequired)
          this.Invoke(new Action(MyWindowMethod));
       else
       {
          //main logic
       }
    }
    
    //Input but no return; SetTitle("My Title")
    public void MyWindowMethod2(string input)
    {
       if(InvokeRequired)
          this.Invoke(new Action<string>(MyWindowMethod2), input);
       else
       {
          //main logic
       }
    }
    
    //inputs and outputs; custom methods, advanced graphics
    public string MyWindowMethod3(string input)
    {
       if(InvokeRequired)
          return (string)(this.Invoke(new Func<string, string>(MyWindowMethod3), input));
    
       //No else required; the return makes it redundant
       //main logic   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently joined a new developing project building a thick client application using
In have my application configured in asp.net. I am using ajaxcontroltoolkit. After building the
I recently started looking into building web applications using .NET MVC and I stumbled
I am building a .NET WinForms based application (currently using .NET 3.5). The application
I'm building an ASP.net application using Visual Studio Web Developer 2010 Express and have
I've started building a ASP net MVC 2.0 web application using SQL Express 2008
I am building a search feature in an asp.net application and I am using
I have a C# 2010 WinForms application using .Net Framework 4. I am using
We are currently building the framework for developing a C# .net application using visual
I am building a Web Application using asp.net (C#). I come from windows forms

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.