Geeky Guy
Geeky Guy

Reputation: 9399

Thread sleeping in a web page - but without the Thread class

I need to have a web page wait for a few seconds before doing something. Let's say that for "security" reasons I can't use the Thread class, so I can't call its Sleep method (don't ask).

I McGyvered this solution:

DateTime foo = DateTime.Now.AddSeconds(5);

while (DateTime.Now < foo) {
    /*noop*/
}

It did work in a Console application, and in a simple page I did just now. I have half a heart to use this but something lodged the darkest corners of my shadow self keeps telling me I am only paving the way to my own doom by doing this. I feel there is a catch to it, but I can't tell what it is.

Is it safe to use? Is it sane?

Upvotes: 1

Views: 333

Answers (1)

usr
usr

Reputation: 171218

Try this:

Mutex m = new Mutex();
w.WaitOne();
m.WaitOne(TimeSpan.FromSeconds(5));

This is a hack of course. But your requirement is to not use Thread.Sleep.

Alternatively, call a web service that sleeps for 5 seconds. That way you at least don't burn CPU in your busy loop.

Upvotes: 2

Related Questions