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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:18:39+00:00 2026-06-08T05:18:39+00:00

I am using an external component which periodically shoots events from a worker thread.

  • 0

I am using an external component which periodically shoots events from a worker thread. In my event handler I use a Dispatcher to invoke some method on the main thread. This works nicely…

private void HandleXYZ(object sender, EventArgs e)
{
    ...
    if(OnTrigger != null)
        dispatcher.Invoke(OnTrigger, new TimeSpan(0, 0, 1), e);
}

However, when the program shuts down and the external component Dispose()s, the program sometimes hangs (and can only be seen and killed in the task manager).

When I look at what is happening it looks like “the component” is waiting for the event to return on the main thread (it stays in the Dispose() method), while the worker thread waits for the dispatcher to invoke the mentioned call to the main thread (it hangs in the dispatcher.Invoke-line).

For now I solved the shutdown problem by adding a timeout to the Invoke, which seems to work but feels wrong.
Is there a cleaner way to do something like this? Can I force the main thread to take some time for jobs from other threads before shutting down?

I have tried to “disconnect” the event before shutting down, but that does not help, because the dispatcher is(could be) already waiting, when the program start to shut down…

PS: external component means here that I do not have access to the source code…

  • 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-08T05:18:41+00:00Added an answer on June 8, 2026 at 5:18 am

    Yes, this is a common source of deadlock. It hangs because the dispatcher exited the dispatcher loop it won’t respond to Invoke requests anymore. A quick cure is to use BeginInvoke instead, it doesn’t wait for the invoke target to finish executing. Another quickie is to set the worker thread’s IsBackground property to True so the CLR will kill it.

    These are quick fixes and they may well work for you. Certainly on your dev machine, but if you have a nagging feeling that it may still go wrong then you’re right, not observing a deadlock or threading race does not prove they are not present. There are two “good” ways to do it completely safely:

    • don’t allow the main thread to exit until you are sure that the worker thread terminated and can no longer raise events. This answer shows the pattern.

    • terminate the program forcefully with Environment.Exit(). This is very crude but very effective, a sledgehammer you’ll only reach for when you have a heavily threaded program where the UI thread is only second citizen. Odd as this may sound as a suitable approach, the new C++ language standard has elevated it to a supported way to terminate a program. You can read more about it in this answer. Do note how it allows for cleanup functions to be registered, you’ll have to do something similar with, say, the AppDomain.ProcessExit event. Focus on the first bullet before you do this.

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

Sidebar

Related Questions

I'm using an external native COM component in my C# .NET application. This COM
I am writing the c# component of a .aspx page which an external site
I am trying to clear images using external jquery file but it's not showing
I'm using an external API to receive xml and serializing this to an object
I'm using an external swf containing a number of font classes to load and
Instead of using an external web-based Mercurial host, I want to set one up
I am using the external library IMAPClient. When the login fails, i see this
This is my first time using an external library, and I'm a bit nervous
In my application I'm using an external library (Batik 1.7) that is made up
One of the side-effects of using an external Subversion repository was getting automatic offsite

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.