Michael A
Michael A

Reputation: 5840

How can I tell when a thread have finished when using a thread pool?

My thread:

public void main_news_thread(MainApplication main)
{
   ThreadPool.QueueUserWorkItem(p => check_news(validrsslist, 0));
}

I call this thread every interval of time...

How can I know when the thread finishes so I can call two other methods which deal with the GUI? How can I refer to this threadpool thread?

Upvotes: 2

Views: 474

Answers (2)

Marc Gravell
Marc Gravell

Reputation: 1062820

Since you are talking about UI, you might want to look at BackgroundWorker, which offers a RunWorkerCompleted event that fires on the UI thread, and indicate success/failure/cancel etc.

http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker_events.aspx

Personally, though, I'd just run a callback method at the end of my worker code (remembering to switch back to the UI thread, via Dispatcher.Invoke in WPF or this.Invoke in winforms).

Upvotes: 4

Femaref
Femaref

Reputation: 61437

You can execute the methods in the thread itself (you have to take care of invoking yourself to access the gui thread):

ThreadPool.QueueUserWorkItem(p => {
                                    check_news(validrsslist, 0);
                                    //do something after the task is finished
                                  });

Upvotes: 1

Related Questions