I'm stuck in ASP.NET 3, which means I don't get to use the Task asynchronous programming model. In ASP.NET 4 forward, I think I can just safely do await Task.Delay(n).ConfigureAwait(false);, but that's not an option here.
I want to purposely create a one-second delay before responding in ASP.NET, but I want to make sure I'm only affecting the current request and not delaying other users. Thread pools and the like have never been my strong point.
If I use Thread.Sleep(1000), it blocks the current thread - does that mean only this particular request? Or are other requests potentially on the same thread and getting blocked, particularly if there is a decent load of traffic?
Does Task.Delay(1000).Wait() behave the same way?
I've hit deadlocks in the past doing this kind of thing as well, which I never really understood, so would these potentially cause deadlocks with other requests?
I have two scenarios:
First, just inside the controller action:
public ActionResult DoSomething() {
DoSomethingElse();
Thread.Sleep(1000); // Or Task.Delay(1000).Wait() or whatever
return something;
}
I also need both this in a separate task that I kick off from a controller action, like a fire-and-forget, which I call with something like:
Task.Run(() => RunProcess(processID))
.ContinueWith(task =>
{
if (task.IsFaulted) { /**/ } else { /**/ }
}
});
void RunProcess(Guid processID) {
DoAnotherThing();
Thread.Sleep(1000); // or Task.Delay(1000).Wait() or whatever else
}