views:

2453

answers:

9

I'm doing some code cleanup, and I came across a few instances of

System.Threading.Thread.Sleep(2000);

These are all in Button Click events.

I can't think of any reason why this would be in production code?

Am I missing something?

EDIT --

Full Code Block (some things changed, but steps are the same) -- And yes, I think it sucks the way it's done too ha

    protected void btnSubmit_Click(object sender, ImageClickEventArgs e)
{
    try
    {
        System.Threading.Thread.Sleep(2000);

        string guid = Profile.Auth.GUID;

        activity act = new activity();

        try
        {
            if (checkbox.Checked)
            {
                add.activity(true);

            }
            else
            {
                act.AddActivity(false)
            }

            LoadData();
        }
        catch (Exception ex)
        {
            Response.Write(ex);
        }
    }
    catch { }
}
+3  A: 

It looks like a version of the speed-up loop. There are no standard reasons for this - most likely someone was compensating for some sort of timing issue.

Andrew Hare
+1 for the link!
RichardOD
+1  A: 

The only reason would be to slow things down (maybe for testing?), so I don't think it should be in production.

Otávio Décio
+3  A: 

A possible reason is a qick hack to make a short delay before displaying the result of something.

The problem with having a Sleep in the user interface code is that the user interface gets unresponsive. If you want a delay like that you would use a timer control. It's a bit more work, but the user experience is better in the end.

Guffa
Turns out the sleep was put in so that a "loading" screen would be visible, so the user knew something was happening. Kind of sad that with AJAX calls now, we intentionally slow thing down so the user can see what's going on. The original dev apprently didn't know about the progress control and DispalyAfter="0"
Jack Marchetti
A: 

I have heard of some people putting in sleep code to compensate for animations/visual effects on the page (progress bars, etc.) after an event is called.

Troggy
+1  A: 

I've used Thread.Sleep for waiting while polling for other actions to finish (pass something off for calculation, poll for completion every quarter second). That said, I can't think of a reason to have: that long of a pause, in a button handler, in a web app. Can't really judge without surrounding code though.

CoderTao
This type of task would be better suited to using a Timer.
Dan Herbert
@Dan I disagree; but mostly because of context. At the library level, calculation is treated as a synchronous call; adding a Timer into the mix breaks the program flow; going from single thread of execution to odd psuedo-multi-thread... Now; if we were pooling resources, had a single 'monitor thread' that did polling, etc; that would be a good spot for a Timer.
CoderTao
A: 

Does the system trigger any back end processing service before going to sleep and is looking for the updated data / cache to be invalidated after the sleep?

Ramesh
Not that I've seen. Part of me thinks they put in a delay to magically "speed it up" a few months down the road.
Jack Marchetti
@JackM: Like the cinder block that you carry around, so that you can drop it when you need to run faster. ;)
Guffa
+1  A: 

Maybe they were copying and pasting code, like this article that uses Thread.Sleep on the Code Project.

This does sound like an entry for daily WTF!

RichardOD
+1  A: 

I've seen people use a sleep to delay the request/response cycle of password submissions and thereby prevent brute force attacks. Of course a lockout policy would acheive the same thing rather than temporarily pulling the plug on the whole thread.

A: 

I am not sure of that code, put it another way, this code System.Threading.Sleep(0) causes the CPU to context switch, i.e. flush the code..but for 2000 milli-seconds or 2 seconds, sounds like the programmer was waiting for a period before obtaining the guid from another class presumably called Profile....it smells of a race condition in there, I would question why wait for 2 seconds before proceeding....

Just my 2cents,

Hope this helps, Best regards, Tom.

tommieb75
the programmer added it, so that the "loading" screen would appear. it was running too fast, so the load wasn't being shown. i know, ridiculous. Also, Profile is just the built in asp.net membership class.
Jack Marchetti