how to put delay in function at some point.

Posted on 2006-06-06
Last Modified: 2012-06-21
Hi experts,

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

How to use this in C#

Thanks in advance
Question by:avinash_takale
    LVL 12

    Accepted Solution

    using System;
    using System.Threading;

    class Test
    public Test()

    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    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);
                    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);

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now