开发者

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开发者_Go百科 with the GUI? How can I refer to this threadpool thread?


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).


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
                                  });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜