Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

Is there any way of doing this ?

like image 758
Rajeev Kumar Avatar asked Dec 26 '22 12:12

Rajeev Kumar


1 Answers

At least two possible solutions:

BackgroundWorker

Use a BackgroundWorker to execute your code, and use the RunWorkerCompleted event to execute the code that is run after completion.

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 .

Tasks (.NET 4.0 and above)

Use a Task object, and use the ContinueWith method to define the code that needs to be executed after completion of the first task.

like image 110
Rotem Avatar answered Jan 19 '23 16:01

Rotem