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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:58:29+00:00 2026-05-16T10:58:29+00:00

The following code demonstrates my dilemma. The code creates a background thread which processes

  • 0

The following code demonstrates my dilemma. The code creates a background thread which processes something, then Invokes the UI thread with the result.

It may throw an exception if the background thread calls Invoke on the form after the form has closed. It checks IsHandleCreated before calling Invoke, but the form might close after the check.

void MyMethod()
{
    // Define background thread
    Action action = new Action(
        () =>
        {
            // Process something
            var data = BackgroundProcess();

            // Try to ensure the form still exists and hope
            // that doesn't change before Invoke is called
            if (!IsHandleCreated)
                return;

            // Send data to UI thread for processing
            Invoke(new MethodInvoker(
                () =>
                {
                    UpdateUI(data);
                }));
        });

    // Queue background thread for execution
    action.BeginInvoke();
}

One solution might be to synchronize FormClosing and every call to Invoke, but that doesn’t sound very elegant. Is there an easier way?

  • 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-16T10:58:30+00:00Added an answer on May 16, 2026 at 10:58 am

    Yes, there’s a race here. A takes a good millisecond before the target starts running. It will work ‘better’ if you use Control.BeginInvoke() instead, the form’s Dispose() implementation will empty the dispatch queue. But that’s still a race, albeit that it will strike very rarely. Your code as written in the snippet doesn’t require Invoke().

    The only clean fix is to interlock the FormClosing event and to delay the close until you got confirmation that the background thread is completed and can’t be started again. Not easy to do with your code as is since that requires a ‘completed’ callback so you can really get the form closed. BackgroundWorker would be a better mousetrap. The Q&D fix is to catch the ObjectDisposedException that BeginInvoke will raise. Given how rare this will be when you use BeginInvoke(), that ugly hack could be acceptable. You just can’t test it 🙂

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

Sidebar

Ask A Question

Stats

  • Questions 516k
  • Answers 516k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It strikes me as possible that you have too many… May 16, 2026 at 6:45 pm
  • Editorial Team
    Editorial Team added an answer In your usercontrol write this method protected void MyMethod() {… May 16, 2026 at 6:45 pm
  • Editorial Team
    Editorial Team added an answer One more option (if the size is known beforehand) -… May 16, 2026 at 6:45 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

The following code demonstrates a misleading situation in which data is committed to the
The following code demonstrates a weird problem I have in a Turbo C++ Explorer
I'm trying to capture the queries which my code submits to the database by
C# 4 in a Nutshell (highly recommended btw) uses the following code to demonstrate
I have been using the following code (with jQuery v1.4.2) to set the 'selected'
Following code works def lab Proc.new { return foo1 }.call return foo2 end puts
Following code does compile whereas name aNumber is not declared before use. class A
following code behaves strange (at least for me): int testValue = 1234; this.ConversionTest( testValue
The following code puts a cube at (0, 0, 0) and another at (0,
I've got the following classes: public class SupplierCategory : IEquatable<SupplierCategory> { public string Name

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.