How to complete thread completion

I am creating a new thread to call a function in it.

Thread th = new Thread(My_Function); th.start(); 

I want to do something after completing this thread execution.

Is there any way to do this?

+4
source share
2 answers

At least two possible solutions:

Backgroundworker

Use BackgroundWorker to execute your code, and use RunWorkerCompleted to execute code that runs after completion.

A BackgroundWorker wraps an event-based asynchronous template in a very easy-to-use mechanism, complete with progress and cancellation reports. See BackgroundWorker Tutorial and this answer.

Tasks (.NET 4.0 and higher)

Use the Task object and use ContinueWith to determine the code that should be executed after the completion of the first task.

+10
source

You can use something like that

 if(th.isAlive()) {...} 
-2
source

All Articles