Search code examples
c#windows-phone-7.1

Thread sleep doesn't work well on Windows Phone 7


I want to put my program to a sleep for a couple of seconds. When I use thread.sleep(x) entire program doesn't respond. According to this article -> http://msdn.microsoft.com/en-us/library/hh184840(v=VS.92).aspx application which is not responsible for 3 seconds doesn't pass the certification :/ (I have to wait 5 seconds).


Solution

  • You need to rethink what you are trying to do with your pause. It's a bit hard to say given that we don't have much info about what you try to achieve with this pause, but I'll give an example here to illustrate how you can get the wanted effect without having to pause the entire app (which you should never do).

    Say you want to download file A, then wait 5 sec ,then download file B. You do this by running something like this:

    var file = downloadFileA();
    new Thread((ThreadStart)delegate
    {
        Thread.Sleep(5000);
        downloadFileB();
    });
    

    Also, it's prefferable to make the actual download-calls async (if it is downloading you are doing), just to make sure that you don't feeze the GUI.

    Alternativly, you can do both the downloads in the background-thread like this:

    new Thread((ThreadStart)delegate
    {
        var file = downloadFileA();
        Thread.Sleep(5000);
        downloadFileB();
    });