In ASP.NET MVC, I have a controller action that calls a function (DoWork) in a thread. That function uses a callback function (CallBackFunction) that is also in the controller. My question: Since DoWork is called in a thread, will the callback function (CallBackFunction) still exist when it will be called by the manager object? If yes, can you explain me why? In my mind, since DoWork is called in a thread, the controller instance may not exists anymore, because no function is running, DoWork is in a separated thread... am I wrong? Is there a chance that it may be garbage collected or destroyed? What is the lifespan of the Controller class?
public ActionResult ControllerAction()
{
//Starts a new thread
new Thread(DoWork).Start();
}
public void DoWork()
{
BusinessLayer.Manager manager = new BusinessLayer.Manager();
// Will CallBackFunction always be available, even if DoWork is called in a thread?
manager.OnStepDone += CallBackFunction;
manager.DoLongRunningActionWithManySteps();
}
public void CallBackFunction(string stepInfo)
{
//do something with stepInfo....
}