1

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
}

2 Answers 2

2

Thread.Sleep is probably ok. But it depends on your load and what the rest of the request is doing.

ASP.NET does assign a separate thread per request, so if you sleep on one, it won't affect other requests.

That said, there is a maximum number of threads. By default it is 20 thread per CPU core, but it is configurable. See maxWorkerThreads in the processModel Element (ASP.NET Settings Schema) documentation.

So you would only run into issues if you are getting more than 20x[your CPU cores] per second. Then introducing a one second delay in every request would cause the request queue to back up if your load is that high.

You would also have to take into account what the rest of the request is doing. If you have 1 second sleep + 1 second of processing time, then you will start having problems if your load is 10x[your CPU cores] per second. So it's worth timing how long the whole request takes if you think your load might be high.

You can raise the maxWorkerThreads value if you think you might have a problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Great, that sounds like what I was looking for. I'll dig more into the processModel, but since this is something I'd use rarely, and not a bunch simultaneously, it seems like blocking one out of 40 threads or whatever my core multiplier is would not cause too much trouble. Thank you mucho.
0

As you mentioned Sleep function of Thread class, it's the exact thing you need and it just sleep the current thread for the amount of time you set, but about dotnet tasks, it's different and dotnet get the thread and use it for other requests until the delay time expires, then one of thread handle the rest of your function. some of other ways to handle this kinda works ia busy waiting.. Loops that is not good.

Thread sleep: https://learn.microsoft.com/en-us/dotnet/api/system.threading.thread.sleep?view=netcore-3.1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.