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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:58:04+00:00 2026-05-20T11:58:04+00:00

Anytime the beginner asks something like: How to update the GUI from another thread

  • 0

Anytime the beginner asks something like: How to update the GUI from another thread in C#?, the answer is pretty straight:

if (foo.InvokeRequired)
{
    foo.BeginInvoke(...)
} else {
    ...
}

But is it really good to use it? Right after non-GUI thread executes foo.InvokeRequired the state of foo can change. For example, if we close form right after foo.InvokeRequired, but before foo.BeginInvoke, calling foo.BeginInvoke will lead to InvalidOperationException: Invoke or BeginInvoke cannot be called on a control until the window handle has been created. This wouldn’t happen if we close the form before calling InvokeRequired, because it would be false even when called from non-GUI thread.

Another example: Let’s say foo is a TextBox. If you close form, and after that non-GUI thread executes foo.InvokeRequired (which is false, because form is closed) and foo.AppendText it will lead to ObjectDisposedException.

In contrast, in my opinion using WindowsFormsSynchronizationContext is much easier – posting callback by using Post will occur only if thread still exists, and synchronous calls using Send throws InvalidAsynchronousStateException if thread not exists anymore.

Isn’t using WindowsFormsSynchronizationContext just easier? Am I missing something? Why should I use InvokeRequired-BeginInvoke pattern if it’s not really thread safe? What do you think is better?

  • 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-20T11:58:05+00:00Added an answer on May 20, 2026 at 11:58 am

    WindowsFormsSynchronizationContext works by attaching itself to a special control that is bound to the thread where the context is created.

    So

    if (foo.InvokeRequired)
    {
        foo.BeginInvoke(...)
    } else {
        ...
    }
    

    Can be replaced with a safer version :

    context.Post(delegate
    {
        if (foo.IsDisposed) return;
        ...
    });
    

    Assuming that context is a WindowsFormsSynchronizationContext created on the same UI thread that foo was.

    This version avoid the problem you evoke :

    Right after non-GUI thread executes foo.InvokeRequired the state of foo can change. For example, if we close form right after foo.InvokeRequired, but before foo.BeginInvoke, calling foo.BeginInvoke will lead to InvalidOperationException: Invoke or BeginInvoke cannot be called on a control until the window handle has been created. This wouldn’t happen if we close the form before calling InvokeRequired, because it would be false even when called from non-GUI thread.


    Beware of some special cases with WindowsFormsSynchronizationContext.Post if you play with multiple message loops or multiple UI threads :

    • WindowsFormsSynchronizationContext.Post will execute the delegate only if there still is a message pump on the thread where it was created. If there isn’t nothing happens and no exception is raised.
      Also if another message pump is later attached to the thread (Via a second call to Application.Run for example) the delegate will execute (It’s due to the fact that the system maintain a message queue per thread without any knowledge about the fact that someone is pumping message from it or not)
    • WindowsFormsSynchronizationContext.Send will throw InvalidAsynchronousStateException if the thread it’s bound to isn’t alive anymore. But if the thread it’s bound to is alive and doesn’t run a message loop it won’t be executed immediately but will still be placed on the message queue and executed if Application.Run is executed again.

    None of these cases should execute code unexpectedly if IsDisposed is called on a control that is automatically disposed (Like the main form) as the delegate will immediately exit even if it’s executed at an unexpected time.

    The dangerous case is calling WindowsFormsSynchronizationContext.Send and considering that the code will be executed: It might not, and there is now way to know if it did anything.


    My conclusion would be that WindowsFormsSynchronizationContext is a better solution as long as it’s correctly used.

    It can create sublte problems in complex cases but common GUI applications with one message loop that live as long as the application itself will always be fine.

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

Sidebar

Related Questions

I use properties pretty much anytime my classes need iVars. For retained properties, I
Question Is there a way to have a method that will always run anytime
I'm doing some data analysis in Matlab, and anytime I call the hold function
Is VBA going to go away any time soon, like VB6 has? Should I
I am a beginner to webservice concepts. Now I know how to deploy webservice
Anytime I have to re-import my projects into Eclipse (if I reinstalled Eclipse, or
http://www.ama3.com/anytime/ $(.pick_date).AnyTime_picker({ format: %W, %M %D, %z }); This is code to initialize AnyTime
It seems to me that anytime I come across internal calls or types, it's
I've been having this problem where anytime I send a 0xA through an RS-232
I want to make it so that anytime the db is queried for an

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.