如何获取线程执行完成事件 [英] How to get thread execution complete event
问题描述
我正在创建一个新线程以在其中调用函数.
I am creating a new thread to call a function in it.
Thread th = new Thread(My_Function);
th.start();
我想在此线程执行完成时做些事情.
I wanna do something on completion of this thread execution.
有什么办法吗?
推荐答案
至少两个可能的解决方案:
At least two possible solutions:
BackgroundWorker
使用 BackgroundWorker
执行您的代码,然后使用 RunWorkerCompleted
事件,以执行在完成后运行的代码.
Use a BackgroundWorker
to execute your code, and use the RunWorkerCompleted
event to execute the code that is run after completion.
A BackgroundWorker
将基于事件的异步模式包装到一个非常易于使用的机制中,并带有进度报告和取消功能.请参见 BackgroundWorker教程和<一个href ="https://stackoverflow.com/questions/6481304/how-to-use-a-backgroundworker">此 SO答案.
A BackgroundWorker
wraps the event based asynchronous pattern into a very easy to use mechanism, complete with progress reporting and cancellation. See this BackgroundWorker tutorial and this SO answer .
任务(.NET 4.0及更高版本)
使用 Task
对象,然后使用 ContinueWith
方法,用于定义在完成第一个任务后需要执行的代码.
Use a Task
object, and use the ContinueWith
method to define the code that needs to be executed after completion of the first task.
这篇关于如何获取线程执行完成事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!