I’m seeing this code in a project and I wonder if it is safe to do:
(ASP.NET MVC 2.0)
class MyController
{
void ActionResult SomeAction()
{
System.Threading.Thread newThread = new System.Threading.Thread(AsyncFunc);
newThread.Start();
}
void AsyncFunc()
{
string someString = HttpContext.Request.UrlReferrer.Authority + Url.Action("Index", new { controller = "AnotherAction" } );
}
}
Is the controller reused, possibly changing the content of HttpContext.Request and Url, or is this fine (except for not using the thread pool).
Thanks for info!
Even if this is valid and works fine now, it just seems risky. The API and/or underlying implementation could always change in a future version, which might cause this code to break.
A much better practice is to pass any required data to the new thread in
SomeActionwhen it is being spawned. For example, by usingParameterizedThreadStartas demonstrated in Passing Parameters to Threads.