Solved

Is it possible to delay for less than a second?

Posted on 2011-02-12
5
383 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

863 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

23 Experts available now in Live!

Get 1:1 Help Now