I want to insert a sleep (aka throttle, delay, tarpit, dwell) in an ASP.net application (imagine something like failed logon attempt escalating delay).
protected void Page_Load(object sender, EventArgs e)
{
Int32 sleepyTime = GetSleepyTime(Request);
if (sleepyTime > 0)
System.Threading.Thread.Sleep(sleepyTime);
//...Continue normal processing
}
I want all remaining processing to continue as normal; i just want the user agent to suffer.
The problem is that ASP.net uses a ThreadPool to handle requests. If i were to Sleep
for 5, 10, 30 seconds, i would be eating up a valuable limited resource.
I assume it needs to be something like:
protected void Page_Load(object sender, EventArgs e)
{
Int32 sleepyTime = GetSleepyTime(Request);
if (sleepyTime > 0)
ABetterKindOfSleep(sleepyTime);
//...Continue normal processing
}
private void ABetterKindOfSleep(int milliseconds)
{
await SleepAsync(milliseconds);
}
private async void SleepAsync(int milliseconds)
{
System.Threading.Thread.Sleep(milliseconds);
}
But never having written any async/await code, and not understanding the logic behind where the async
and await
go, or why, or even if it can be used to run async code: i don't know if it can be used to run async code.