Wait fn/sleep/delay

Posted on 2005-03-15
Medium Priority
Last Modified: 2009-07-29
I need to create a delay of few seconds in my code.
Is there any efficient function to do that.
Question by:rishaan
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13546995
Here are two different ways to create a delay.  The first makes the app delay in one line but then the app becomes unresponsive during that interval.  Hit the button and then try moving the form.  It will not respond until the interval is over.  Then second approach gives you a delay while keeping the app responsive.  Another alternative would be to use a Timer and boolean flag:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Button1.Enabled = False

        System.Threading.Thread.Sleep(5000) ' five seconds

        Button1.Enabled = True
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Button2.Enabled = False

        Dim targetTime As DateTime = DateTime.Now().AddSeconds(5)
        Dim ts As TimeSpan
            Application.DoEvents() ' keep application responsive
            System.Threading.Thread.Sleep(50) ' very small delay to reduce CPU Usage
            ts = targetTime.Subtract(DateTime.Now)
        Loop While ts.TotalSeconds > 0

        Button2.Enabled = True
    End Sub

Author Comment

ID: 13547393
My application(service) is actually calling some other app and it has to wait till the other app takes on all the locks
So which one is good in my case??
Will thread.sleep etc cause any problem...I havent used threads...
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13547411
>> it has to wait till the other app takes on all the locks

How do you know when this other app is done?  Do you need to wait for it to close?

Author Comment

ID: 13547885
no. I don't have to wait till it is done. But I have to give a delay to make sure the locks released by my app are taken by the other app.
LVL 86

Accepted Solution

Mike Tomlinson earned 200 total points
ID: 13547975
With a service that has no user interface I would think the first method with only a call to System.Threading.Thread.Sleep() would be fine.

Of course, there is no guarantee that the locks will be taken by the end of your sleep interval...

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

771 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