Solved

Is it possible to delay for less than a second?

Posted on 2011-02-12
5
412 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
[X]
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
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 86

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

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

623 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