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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:23:49+00:00 2026-05-28T19:23:49+00:00

I am having a problem with multi threading in C#. I use an event

  • 0

I am having a problem with multi threading in C#.
I use an event to update a label in a form from another thread, for which I need to use the Invoke() command of course.
That part is also working fine.
However, the user can close the form and here the program can crash if the event is sent at an unfortunate time.

So, I thought I would simply override the Dispose() method of the form, set a boolean to true within locked code, and also check that boolean and invoke the event in locked code.

However, every time I close the form the program freezes completely.

Here are the mentioned parts of the code:

private object dispose_lock = new object();
private bool _disposed = false;

private void update(object sender, EventArgs e)
{
    if (InvokeRequired)
    {
        EventHandler handler = new EventHandler(update);
        lock (dispose_lock)
        {
            if (_disposed) return;
            Invoke(handler); // this is where it crashes without using the lock
        }
        return;
    }

    label.Text = "blah";
}

protected override void Dispose(bool disposing)
{
    eventfullObject.OnUpdate -= update;
    lock (dispose_lock) // this is where it seems to freeze
    {
       _disposed = true; // this is never called
    }
    base.Dispose(disposing);
}

I hope anyone here has any idea what is wrong with this code.
Thank you in advance!

  • 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-28T19:23:50+00:00Added an answer on May 28, 2026 at 7:23 pm

    I really would go simple here. Instead of implementing tricky thread-safe code, I would simply catch the exception and do nothing if it fails.

    Assuming it’s a ObjectDisposedException:

    try
    {
        this.Invoke(Invoke(handler));
    }
    catch (ObjectDisposedException)
    {
        // Won't do anything here as
        // the object is not in the good state (diposed when closed)
        // so we can't invoke.
    }
    

    It’s simpler and pretty straightforward. If a comment specifies why you catch the exception, I think it’s OK.

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

Sidebar

Related Questions

I'm having a problem with multi-threading in Java. I need to compare a large
I am having a problem with doxygen style multi-line comments with emacs indent feature
I am having strange problem after installing multi-site drupal environment on shared host. When
I'm having a problem with interlocked Monitor.Wait and Monitor.Pulse in a multi-threaded TCP server.
I'm having a problem finding out which NSTextfield is focused. I am building a
Folks, I am having a problem with a c#, multi-project solution in Visual Studio
I am having problem in entering multi-line commands in ghci. The following 2-line code
I'm having a problem lately that's driving me crazy. I have a multi-dimensional array
I'm having a problem with curl_multi_* , I want to create a class /
Having problem with the middle Div not expanding to the width http://acs.graphicsmayhem.com/images/middiv.jpg Ok, how

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.