Calling a method when thread terminates Calling a method when thread terminates multithreading multithreading

Calling a method when thread terminates


The BackgroundWorker class exists for this sort of thread management to save you having to roll your own; it offers a RunWorkerCompleted event which you can just listen for.


Edit to make it call a helper method so it's cleaner.

thread = new Thread(() => { Synchronize(); OnWorkComplete(); });...private void OnWorkComplete(){    Close();}


If you have a look at a BackgroundWorker, there is a RunWorkerCompleted event that is called when the worker completes.

For more info on BackgroundWorkers Click Here

Or

You could add a call to a complete function from the Thread once it has finished, and invoke it.

void Synchronize(){    //DoWork();    //FinishedWork();}void FinishedWork(){if (InvokeRequired == true)  {  //Invoke  }else  {  //Close  }}