If you want to restrict the execution to one at a time, then you'll need a static lock object:
public class HomeController : Controller {
private static object _lockobject = new object();
And then:
public ActionResult Index() {
String str;
lock (_lockobject)
{
str = dosomething();
}
return View();
}
If you need a timeout, then maybe use Monitor.TryEnter
instead of lock
:
public ActionResult Index() {
String str;
if (Monitor.TryEnter(_lockobject, TimeSpan.FromSeconds(5)))
{
str = dosomething();
Monitor.Exit(_lockobject);
}
else str = "Took too long!";
return View();
}