views:

444

answers:

3

I've got following problem: I have monitoring class, which is running it's own thread that writes from queue into file (so the main application doesn't have to wait on IO). But, when main application thread ends (control flow runs after it's last line), the monitor thread ends too, even if it is still running (full queue).

Is there any way, without modifying main thread to wait till the worker thread is done? C#.

EDIT: I cannot modify main thread. I'm writing only 'support' code for huge application with given API (one static method containing what shall I write, where is read from configuration), there is no way how to change threads, main app must not depend on my code.

+1  A: 

Switch them around. Make your main thread the one that monitors, and spawn the worker threads (write from Q to file) from there.

Or have the main thread startup threads for monitor and work, and then have the main thread spin and wait (loop until it gets abort/complete notifications from the other threads)

StingyJack
+1  A: 

You can use a ManualResetEvent and call WaitOne() at the end of your main execution thread. When the worker thread is done simply signal the ManualResetEvent and it will continue execution of the main thread.

gwhitake
+2  A: 

The answer is at http://stackoverflow.com/questions/1280723/c-threading-lock-confusion

Steven Sudit
THis would be true if he was using ThreadPool threads, but he didnt say one way or another.
StingyJack
in fact, i set the IsBackground property, unaware of it's real meaning. my bad, thanks.
Yossarian