locked
Delay (anologue of Thread.Sleep ) RRS feed

  • Question

  • Hi, guys. I need to execution some action in the application one after the other with a delay. If I use the timer, then the condition of execution action one after the other is not satisfied. If I use the Thread.Sleep, then during the execution of Thread.Sleep() there is a black screen. The delay time longer than five seconds and it is critical, because during the delay aplication must show the normal screen. I need an alternative of Thread.Sleep(). Or an explanation (or link to additional material) of how to use the Thread.Sleep() for WP7.
    Wednesday, January 26, 2011 12:27 PM

Answers

  • I always try to stay away from timers, unless it's for an action that must be performed at regular intervals (note 'regular'). In your case I would use Thread.Sleep in a background thread, and then post back to the UI thread after each item is processed:
    SynchronizationContext context = SynchronizationCOntext.Current;
    Thread t = new Thread(() =>
    {
        // Get info for items...
        foreach (var item in items)
        {
            // Use Send() for synchronous processing
            context.Send((state) =>
            { 
                 // This executes on the UI thread, make sure you don't use the item variable here!!
                 var temp = (ItemType)state;
                 PerformAction(temp);
            }, item);
            Thread.Sleep(200);
    
        }
    };
    
    t.IsBackground = true;
    t.Start();
    
    Wednesday, January 26, 2011 3:03 PM

All replies

  • Just keep track of what methods you have executed and use a timer between each one.

    Psudocode:
    // Global  
    bool started = false;  
    int stage = 0;  
    Timer timer;  
     
    update()  
    {  
     
        If (timer > 5 seconds || !started)  
        {  
            started = true;
            switch(stage)  
            {  
               case 0:  
                   timer = 0;  
                   executeStage0();  
                   // Next stage  
                   stage = 1;  
                   timer.start();  
                   break;  
               case 1:  
                   timer = 0;  
                   executeStage1();  
                   // Next stage  
                   stage = 2;  
                   timer.start();  
                   break;  
               //...  
               case 5:  
                   timer = 0;  
                   executeStage5();  
                   // Go back to the start  
                   stage = 0;  
                   timer.start();  
            }  
        }  
     
    You could probably use gameTime instead of the timer.

    Regards
    John
    Wednesday, January 26, 2011 1:00 PM
  • I always try to stay away from timers, unless it's for an action that must be performed at regular intervals (note 'regular'). In your case I would use Thread.Sleep in a background thread, and then post back to the UI thread after each item is processed:
    SynchronizationContext context = SynchronizationCOntext.Current;
    Thread t = new Thread(() =>
    {
        // Get info for items...
        foreach (var item in items)
        {
            // Use Send() for synchronous processing
            context.Send((state) =>
            { 
                 // This executes on the UI thread, make sure you don't use the item variable here!!
                 var temp = (ItemType)state;
                 PerformAction(temp);
            }, item);
            Thread.Sleep(200);
    
        }
    };
    
    t.IsBackground = true;
    t.Start();
    
    Wednesday, January 26, 2011 3:03 PM