Solved

Is it possible to delay for less than a second?

Posted on 2011-02-12
5
369 Views
Last Modified: 2012-05-11
Hi

I am using the following delay Sub to hold my program slightly each time it loops to write a record to multiple locations to give the program time to keep up and update certain viewable fields and a Progress Bar, but i was wondering if it is possible to delay for less than a second? As the second i feel is too long for what it required.

 
Public Sub Delay(ByVal dblSecs As Double)

        Const OneSec As Double = 1.0# / (1440.0# * 60.0#)
        Dim dblWaitTil As Date
        Now.AddSeconds(OneSec)
        dblWaitTil = Now.AddSeconds(OneSec).AddSeconds(dblSecs)
        Do Until Now > dblWaitTil
            Application.DoEvents() ' Allow windows messages to be processed
        Loop

    End Sub

Open in new window

0
Comment
Question by:STP_Martin24
5 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 34879042
Please show the code where you use your Delay sub. Most program statements are blocking, meaning that it does not return executing in the current function until it has returned from the call. Your GUI will not update until after the current function is complete or you place a Application.DoEvents() to pause and update the GUI.
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 34879062
Instead of AddSeconds(), use AddMilliseconds():
 
Public Sub Delay(ByVal intMilliseconds As Integer)
        Dim dt As DateTime = DateTime.Now.AddMilliseconds(intMilliseconds)
        Do Until Now > dt
            Application.DoEvents() ' Allow windows messages to be processed
            System.Threading.Thread.Sleep(50)
        Loop
    End Sub

Open in new window

0
 

Author Comment

by:STP_Martin24
ID: 34879063
Hi

I tried using:

System.Threading.Thread.Sleep(500)

But it did make the program any quickier comapred to Delay(1) - 1 second

Without any Delay's the program does keep up and refreshing the DTable after each new record but after a few it goes to "Not Responding" then the next update is when the program is finished writing but i just wanted to slow it down after each record so it doesn't seem to the user it isn't working if though it is. But like i said i tried the above which should be 500ms and i but a start and end time stamps and there was very little difference and the Delay(1) kept the program moving.

If anyone has anything i'm missing please post and i will be happy to look.
0
 

Author Closing Comment

by:STP_Martin24
ID: 34879088
Well that was unlucky timing as i was writing my last post as you posted yours Idle_Mind, just gave it a quick try and it looks like it works quite nice as it cut 60 record write time from 1min 30ish to about 1 minute, with the DTable Update and Progress Bar Updates working nicely.

Thanks for your quick response
0
 
LVL 3

Expert Comment

by:pasolo
ID: 34879089

If you are doing it all in a single thread you don't need more that a single Application.DoEvents() to dispatch all pending messages in the queue and update the controls (more doevents will not help because nothing else is done than dispatch messages). If you are working with a second thread for the background work then use threading.thread.sleep on that thread after each record reading.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 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

12 Experts available now in Live!

Get 1:1 Help Now