18

A colleague of mine wrote some code that essentially pauses for 1 second before making a webservice call to check the state of a value. This code is written in a controller action of a MVC 4 application. The action itself is not asynchronous.

var end = DateTime.Now.AddSeconds(25);
var tLocation = genHelper.GetLocation(tid);

while (!tLocation.IsFinished && DateTime.Compare(end, DateTime.Now) > 0)
{
    var t = DateTime.Now.AddSeconds(1);
    while (DateTime.Compare(t, DateTime.Now) > 0) continue;

    // Make the webservice call so we can update the object which we are checking the status on
    tLocation = genHelper.GetLocation(tid);
}

It appears to work but for some reason I have some concerns over it's implementation. Is there a better way to make this delay?

NOTE:

  1. We are not using .NET 4.5 and will not change to this in this solution
  2. Javascript scrip options like SignalR are not an option at present

I had thought the question was a good option but he did not take it up and said it wasn't required as what he did works.

How to put a task to sleep (or delay) in C# 4.0?

3
  • I would switch to ajax to query the website instead of blocking the thread on the server for 25 seconds...
    – Timmerz
    Jan 29, 2014 at 1:41
  • Yes, I will recommend that thanks but time might be the decider on this one. Cheers
    – dreza
    Jan 29, 2014 at 2:09
  • Probably using .Net 4.5 already, but I would still mention what the risk of this and Thread.Sleep is. Both block a thread for a period of time during what the thread doesn't do anything but can't be assigned to service another request either. This will lead to blocking HTTP requests at some point because no threads will be available. When that point comes depends on many-many factors, and the number of concurrent users is only one of them, e.g. a site with many ajax requests per user actions will run out of threads with less concurrent users. So don't block threads, especially on a webserver. Nov 17, 2016 at 16:13

1 Answer 1

40

For MVC and your situation, this is sufficient:

System.Threading.Thread.Sleep( 1000 );

A fancy way to do the same thing but with more overhead:

Task.WaitAll( Task.Delay( 1000 ) );

Update:

Quick and dirty performance test:

class Program
{
    static void Main()
    {
        DateTime now = DateTime.Now;

        for( int i = 0; i < 10; ++i )
        {
            Task.WaitAll( Task.Delay( 1000 ) );
        }

        // result: 10012.57xx - 10013.57xx ms
        Console.WriteLine( DateTime.Now.Subtract( now ).TotalMilliseconds );

        now = DateTime.Now;

        for( int i = 0; i < 10; ++i )
        {
            Thread.Sleep( 1000 );
        }

        // result: *always* 10001.57xx
        Console.WriteLine( DateTime.Now.Subtract( now ).TotalMilliseconds );

        Console.ReadLine();
    }
}
6
  • I saw quite a few comments to say that this was a bit no no i.e. using Thread sleep??
    – dreza
    Jan 29, 2014 at 1:37
  • if you have to block on the server I think this option is the best way to go...otherwise as I mentioned in my other comment, use ajax or something to avoid locking up the web threads
    – Timmerz
    Jan 29, 2014 at 1:43
  • 1
    If you really want to wait for 1 second, this is the simplest way. The only real harm it'll cause the thread you are running this on will be blocked for 1 second. If you want to avoid that, create a timer, set Interval to 1000 and do whatever you need to do in the Elapsed event, but that's probably not going to work as I assume you need to return the data to the client.
    – Fayilt
    Jan 29, 2014 at 1:43
  • Yes, we do need to return data. Awesome so it's this or using Ajax in my situation. Cheers.
    – dreza
    Jan 29, 2014 at 2:04
  • 4
    Note that with Thread.Sleep(1000) is nondeterministic. This means that you are not certain that your Thread will indeed sleep for 1000ms, it could be more (but always at least 1000ms). Task.Delay uses a timer in the background so awaiting that will stop the execution for exactly 1000ms. Jan 29, 2014 at 15:56

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.