Suppose I were to have a web application and a view called innerPage for the application who’s model looks something like this:
public class innerPageModel
{
public bool isFirstTime = true;
public List<int> threadIDList;
}
And here is the controller for my innerPage:
public ActionResult innerPage(InnerPageModel model)
{
if (model.isFirstTime == true)
{
Thread t = new Thread(Work);
model.threadIDList.Add(t.ManagedThreadID);
model.isFirstTime = false);
}
System.Diagnostics.Debug.Write((model.threadIDList.Count).toString());
}
Now my innerPage view is being refreshed every 5 seconds and therefore traversing my innerPage controller each time. The problem is that all of the data in my model is being reset after every time I traverse the controller (Everytime it goes inside the if-block, indicating that model.isFirstTime is being reset to true when I only want to traverse that if-block on the first time). Likewise, my model.threadIDList is being reset everytime through.
Is there a way for me to save the data in my model such that it isn’t lost everytime my view refreshes?
The MVC model assumes the controller will be invoked, return an ActionResult and then be done.
It does not seem safe to kick off threads in the controller action.
If you need data to remain between HTTP requests, you can store it in the Session or in Cache, depending on whether the data is per-user or global to the website.
If you need true background processing, look at