• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1216
  • Last Modified:

how to put delay in function at some point.

Hi experts,

I want to put delay just like vb function sleep()

How to use this in C#

Thanks in advance
-Avinash
0
avinash_takale
Asked:
avinash_takale
2 Solutions
 
topdog770Commented:
using System;
using System.Threading;

class Test
{
public Test()
{
    Thread.Sleep(500);
}
}

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
How long a pause do you need?

Sleep() makes your app completely UNRESPONSIVE during the sleep interval...your app won't respond to your users events and won't repaint itself correctly.  This may be unacceptable depending on your application and the length of the delay needed...


You could use a Timer control, or possibly use a polling loop:

        Delay(5); // five second delay



        private void Delay(int DelayInSeconds)
        {
            TimeSpan ts;
            DateTime dt = DateTime.Now.AddSeconds(DelayInSeconds);
            do
            {
                ts = dt.Subtract(DateTime.Now);
                Application.DoEvents(); // keep app responsive
                System.Threading.Thread.Sleep(50); // Reduce CPU usage (keep interval SMALL)
            }
            while (ts.TotalMilliseconds > 0);
        }
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now