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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:07:01+00:00 2026-05-18T04:07:01+00:00

Inspired by my own experience with multithreaded Winforms applications, as well as questions such

  • 0

Inspired by my own experience with multithreaded Winforms applications, as well as questions such as

  • Avoiding the woes of Invoke/BeginInvoke in cross-thread WinForm event handling?
  • Avoid calling Invoke when the control is disposed

I’ve come up with a very simple pattern, whose soundness I would like to verify.

Basically I’m creating (and running throughout the application’s lifetime) a BGW whose sole purpose is the synchronization of invoke requests. Consider:

public MainForm()
{
    InitializeComponent();
    InitInvocationSyncWorker();
}

private void InitInvocationSyncWorker()
{
    InvocationSync_Worker.RunWorkerAsync();
}

private void InvocationSync_Worker_DoWork(object sender, DoWorkEventArgs e)
{
    Thread.Sleep(Timeout.Infinite);
}

void InvokeViaSyncWorker(Action guiAction)
{
    InvocationSync_Worker.ReportProgress(0, guiAction);
}

private void InvocationSync_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    if (IsDisposed) return; //we're in the GUI thread now, so no race condition right?

    var action = (Action) e.UserState;
    action();
}

public void SomeMethodCalledFromAnyThread() //Sample usage
{
    InvokeViaSyncWorker(() => MyTextBox.Text = "Hello from another thread!"));    
}

Granted, it’s not the most economical of approaches (keeping a thread alive like that), but if it works and I haven’t missed anything, it sure is the simplest I’ve seen.

Feedback is highly appreciated !

  • 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-18T04:07:02+00:00Added an answer on May 18, 2026 at 4:07 am

    There’s no need to open a thread just for that. Simply use SynchronizationContext, like so:

    private readonly SynchronizationContext _syncContext;
    
    public MainForm()
    {
        InitializeComponent();
    
        _syncContext = SynchronizationContext.Current;
    }
    
    void InvokeViaSyncContext(Action uiAction)
    {
        _syncContext.Post(o =>
        {
            if (IsHandleCreated && !IsDisposed) uiAction();
        }, null);
    }
    
    public void SomeMethodCalledFromAnyThread() //Sample usage
    {
        InvokeViaSyncContext(() => MyTextBox.Text = "Hello from another thread!"));    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

recently this post inspired me, I want to track my own life, too. I
Inspired by this CodingHorror article, Protecting Your Cookies: HttpOnly How do you set this
Inspired by Raymond Chen's post , say you have a 4x4 two dimensional array,
Inspired by the MVC storefront the latest project I'm working on is using extension
Inspired by another question asking about the missing Zip function: Why is there no
Inspired by this question , I wanted to try my hand at the latest
Inspired by this question and answer , how do I create a generic permutations
Inspired by the question What’s the simplest way to call Http GET url using
Inspired by this question I began wondering why the following examples are all illegal
Inspired by Eric Sink's interview on the stackoverflow podcast I would like to build

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.