Solved

Is it possible to delay for less than a second?

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 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