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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:03:35+00:00 2026-05-11T00:03:35+00:00

I have a console application. A class (let’s say Worker) does some work in

  • 0

I have a console application. A class (let’s say Worker) does some work in a separate thread and throws an event when it finishes. But this never happens because the execution ends instantly. How can I wait for the thread to finish and handle the event after it throws?

static void Main(string[] args) {     Worker worker = new Worker();     worker.WorkCompleted += PostProcess;     worker.DoWork(); }  static void PostProcess(object sender, EventArgs e) { // Cannot see this happening } 

Edit: Corrected the order of the statements but that was not the problem.

  • 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. 2026-05-11T00:03:35+00:00Added an answer on May 11, 2026 at 12:03 am

    You’ve got a race condition, in that the work could finish before you register for the event. To avoid the race condition, change the order of the code so you register for the event before starting the work, then it will always be raised, no matter how fast it finishes:

    static void Main(string[] args) {     Worker worker = new Worker();     worker.WorkCompleted += PostProcess;     worker.DoWork(); } 

    Edit:

    OK the question has been modified, so it looks like what you’re really asking is how to wait for PostProcess to finish executing. There are a couple of ways to do this, but you’ll have to add some more code.

    The easiest way is, because events always execute on the same thread as they are raised, is to call Thread.Join on the thread the Worker class creates, e.g. assuming the thread is exposed as a property:

    worker.Thread.Join(); 

    (Although to be honest I’d probably keep the Thread private and expose a method called something like WaitForCompletion on the Worker class that calls it).

    Alternative methods are:

    1. Have a WaitHandle, probably a ManualResetEvent, in the Worker class which is Set when it completes all its work, and call WaitOne on it.

    2. Have a volatile bool complete field in the Worker class and loop while waiting for it to be set to true, using Thread.Sleep in the loop body (this probably isn’t a good solution, but it is feasible).

    There are probably other options too, but that’s the common ones.

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

Sidebar

Ask A Question

Stats

  • Questions 177k
  • Answers 177k
  • 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 Try this using System.Runtime.InteropServices; const int HT_CAPTION = 0x2; const… May 12, 2026 at 3:30 pm
  • Editorial Team
    Editorial Team added an answer You should add an OnRowCommand event in the GridView and… May 12, 2026 at 3:30 pm
  • Editorial Team
    Editorial Team added an answer You should be able to do somthing like this: //… May 12, 2026 at 3:30 pm

Related Questions

I have a console application. A class (let's say Worker) does some work in
I'm having a bit of difficulty passing a reference type between webservices. My set
I have a GUI application within which i'm spawning a console application using Process
Preamble So, this question has already been answered, but as it was my first
I have a .NET console app running on Windows Server 2003, as a scheduled

Trending Tags

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

Top Members

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.