0

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....
}
Roger Lebrun
  • 501
  • 1
  • 4
  • 6

1 Answers1

0

In order for your thread to do its work and complete, the creating controller instance will need to wait for the thread to finish. That means that you could be holding an HTTP response hostage until that work is complete. Check this out: How to use threads in asp.net?

matt-dot-net
  • 4,204
  • 21
  • 24