locked
Thread.Sleep in Win8 RRS feed

  • Question

  • Where did the Thread.Sleep method go? 
    Tuesday, September 20, 2011 10:46 PM

Answers

  • In C# / VB you'd want to use either a DispatcherTimer callback, or a custom Task + await keyword to check the time and update as needed, yielding to other threads as necessary.


    For Javascript / HTML5 I do not know but that's a great question for Building Metro style apps with HTML5/JavaScript forum!

    -Matt


    SDET : Deployment/Hosting
    Wednesday, September 21, 2011 12:00 AM

All replies

  • Hi Mary,

       This is an intentional change to steer the way applications are developed.  Please check out this article:

            http://msdn.microsoft.com/en-us/library/windows/apps/br230302(v=vs.85).aspx#threading

      In Metro style applications, the goal is to move to a Task oriented instead of Thread oriented model.  Some small part of this is still exposed via the Windows.System.Threading classes but the goal is to move away from explicit Sleep()s like you're asking for.

    Can you describe the scenarios you're hitting that require Sleep?

    Hope this helps,

    Matt


    SDET : Deployment/Hosting

    Tuesday, September 20, 2011 11:22 PM
  • Its really just a test application.  I want to send a tile update every 30 seconds just to mimic things happening in the application.

    Tuesday, September 20, 2011 11:53 PM
  • In C# / VB you'd want to use either a DispatcherTimer callback, or a custom Task + await keyword to check the time and update as needed, yielding to other threads as necessary.


    For Javascript / HTML5 I do not know but that's a great question for Building Metro style apps with HTML5/JavaScript forum!

    -Matt


    SDET : Deployment/Hosting
    Wednesday, September 21, 2011 12:00 AM
  • Maybe you could try Task.Wait(), if that's available.

    Wednesday, September 21, 2011 9:27 AM
  • Maybe you could try Task.Wait(), if that's available.


    Seems to be available, and I vaguely remember Anders using it in his code (in the build session).
    http://blog.voidnish.com
    Wednesday, September 21, 2011 3:18 PM
  • Per Matt's suggestion, if you decide to go the Task route, inside of an async method in C# or Visual Basic, you can use await on a Task.Delay task to asynchronously wait for a period of time.  If you invoke the following LoopAsync method from your UI thread, it'll invoke the DoWork method once every 30 seconds, and during those time intervals the UI will not be blocked:

    async void LoopAsync()
    {
        while(true)
        {
            await Task.Delay(TimeSpan.FromSeconds(30));
            DoWork();
        }
    }

    Wednesday, September 21, 2011 6:48 PM
  • Hi,

      why not a Thread.CurrentThread.Join(30).

    Cheers,

    Julien

    Tuesday, September 27, 2011 7:35 AM
  • Hi,

    I have been trying to use just to sleep simulate a delay in a thread while performing an action to test something.

    It doesn't make sense to remove the call because people will find another way of doing sleeping e.g. one can use a AutoResetEvent  and WaitOne with a time out to achieve the same thing.

    Paul


    jamtoday

    Saturday, May 19, 2012 7:49 PM