locked
How to Sleep/Delay within a "for" loop in C# ? RRS feed

  • Question

  • I would like to have a code for an image slide show. Images should be displayed for a give time, e.g. 5 sec. "Thred.Sleep()" seems not working for this purpose. What is the best way ?

    Thanks

    • Moved by CoolDadTx Monday, September 27, 2010 1:19 PM Not IDE related (From:Visual C# IDE)
    Saturday, September 25, 2010 2:42 AM

Answers

  • Hi There,

    You have to use Application.DoEvents() before Thread.Sleep();

    for example...

     

    for loop.... all images

    {

    show_next_image();

    Application.DoEvents(); //allow windows to execute all pending tasks including your image show...

    Thread.Sleep(5000);

    }

     

    I hope this was helpful.

     

    Cheers,

     

     

     

     

     


    Mubi | www.mrmubi.com | Mark The Best Replies As Answers!
    • Marked as answer by Kira Qian Thursday, October 14, 2010 9:30 AM
    Saturday, September 25, 2010 8:19 PM
  • You can use System.Threading.Timer class for executing a method in specific time interval. This will be more efficent way of doing it.

    When you create a timer, you can specify an amount of time to wait before the first execution of the method (due time), and an amount of time to wait between subsequent executions (period). You can change these values, or disable the timer, using the Change method.

    When a timer is no longer needed, use the Dispose method to free the resources held by the timer. Note that callbacks can occur after the Dispose() method overload has been called, because the timer queues callbacks for execution by thread pool threads. You can use the Dispose(WaitHandle) method overload to wait until all callbacks have completed

    See this example.

    using System;
    using System.Threading;
    
    class TimerExample
    {
      static void Main()
      {
        // Create an event to signal the timeout count threshold in the
        // timer callback.
        AutoResetEvent autoEvent   = new AutoResetEvent(false);
    
        StatusChecker statusChecker = new StatusChecker(10);
    
        // Create an inferred delegate that invokes methods for the timer.
        TimerCallback tcb = statusChecker.CheckStatus;
    
        // Create a timer that signals the delegate to invoke 
        // CheckStatus after one second, and every 1/4 second 
        // thereafter.
        Console.WriteLine("{0} Creating timer.\n", 
          DateTime.Now.ToString("h:mm:ss.fff"));
        Timer stateTimer = new Timer(tcb, autoEvent, 1000, 250);
    
        // When autoEvent signals, change the period to every
        // 1/2 second.
        autoEvent.WaitOne(5000, false);
        stateTimer.Change(0, 500);
        Console.WriteLine("\nChanging period.\n");
    
        // When autoEvent signals the second time, dispose of 
        // the timer.
        autoEvent.WaitOne(5000, false);
        stateTimer.Dispose();
        Console.WriteLine("\nDestroying timer.");
      }
    }
    
    class StatusChecker
    {
      private int invokeCount;
      private int maxCount;
    
      public StatusChecker(int count)
      {
        invokeCount = 0;
        maxCount = count;
      }
    
      // This method is called by the timer delegate.
      public void CheckStatus(Object stateInfo)
      {
        AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;
        Console.WriteLine("{0} Checking status {1,2}.", 
          DateTime.Now.ToString("h:mm:ss.fff"), 
          (++invokeCount).ToString());
    
        if(invokeCount == maxCount)
        {
          // Reset the counter and signal Main.
          invokeCount = 0;
          autoEvent.Set();
        }
      }
    }
    
    
    • Marked as answer by Kira Qian Thursday, October 14, 2010 9:30 AM
    Thursday, October 7, 2010 8:41 AM

All replies

  • Hi There,

    You have to use Application.DoEvents() before Thread.Sleep();

    for example...

     

    for loop.... all images

    {

    show_next_image();

    Application.DoEvents(); //allow windows to execute all pending tasks including your image show...

    Thread.Sleep(5000);

    }

     

    I hope this was helpful.

     

    Cheers,

     

     

     

     

     


    Mubi | www.mrmubi.com | Mark The Best Replies As Answers!
    • Marked as answer by Kira Qian Thursday, October 14, 2010 9:30 AM
    Saturday, September 25, 2010 8:19 PM
  • I am writing to check the status of the issue on your side. Could you please let me know if the suggestion works for you? If you have any questions or concerns, please feel free to let me know. I will be more than happy to be of assistance.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    Thursday, October 7, 2010 7:36 AM
  • You can use System.Threading.Timer class for executing a method in specific time interval. This will be more efficent way of doing it.

    When you create a timer, you can specify an amount of time to wait before the first execution of the method (due time), and an amount of time to wait between subsequent executions (period). You can change these values, or disable the timer, using the Change method.

    When a timer is no longer needed, use the Dispose method to free the resources held by the timer. Note that callbacks can occur after the Dispose() method overload has been called, because the timer queues callbacks for execution by thread pool threads. You can use the Dispose(WaitHandle) method overload to wait until all callbacks have completed

    See this example.

    using System;
    using System.Threading;
    
    class TimerExample
    {
      static void Main()
      {
        // Create an event to signal the timeout count threshold in the
        // timer callback.
        AutoResetEvent autoEvent   = new AutoResetEvent(false);
    
        StatusChecker statusChecker = new StatusChecker(10);
    
        // Create an inferred delegate that invokes methods for the timer.
        TimerCallback tcb = statusChecker.CheckStatus;
    
        // Create a timer that signals the delegate to invoke 
        // CheckStatus after one second, and every 1/4 second 
        // thereafter.
        Console.WriteLine("{0} Creating timer.\n", 
          DateTime.Now.ToString("h:mm:ss.fff"));
        Timer stateTimer = new Timer(tcb, autoEvent, 1000, 250);
    
        // When autoEvent signals, change the period to every
        // 1/2 second.
        autoEvent.WaitOne(5000, false);
        stateTimer.Change(0, 500);
        Console.WriteLine("\nChanging period.\n");
    
        // When autoEvent signals the second time, dispose of 
        // the timer.
        autoEvent.WaitOne(5000, false);
        stateTimer.Dispose();
        Console.WriteLine("\nDestroying timer.");
      }
    }
    
    class StatusChecker
    {
      private int invokeCount;
      private int maxCount;
    
      public StatusChecker(int count)
      {
        invokeCount = 0;
        maxCount = count;
      }
    
      // This method is called by the timer delegate.
      public void CheckStatus(Object stateInfo)
      {
        AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;
        Console.WriteLine("{0} Checking status {1,2}.", 
          DateTime.Now.ToString("h:mm:ss.fff"), 
          (++invokeCount).ToString());
    
        if(invokeCount == maxCount)
        {
          // Reset the counter and signal Main.
          invokeCount = 0;
          autoEvent.Set();
        }
      }
    }
    
    
    • Marked as answer by Kira Qian Thursday, October 14, 2010 9:30 AM
    Thursday, October 7, 2010 8:41 AM
  • thank very much ! this answer helped me alot !!!!!!!!! 
    Friday, September 19, 2014 4:29 PM
  • Thank you so much! I have been trawling Stack Overflow for forever to look for a solution like this, and didn't find anything that worked for my use case. This is the first post I have found which has been useful!
    Monday, October 21, 2019 3:32 AM