Solved

Pause/Wait without Locking up application

Posted on 2003-10-27
2
362 Views
Last Modified: 2010-05-01
I have an application that runs a command or opens a dialog at a user specified interval.  I am using the timer to set the interval.  When I attempt to set the timer's interval above 64 seconds, I get an error "Invalid Property Value."  I thought about using the sleep and tried it.  It successfully works for the interval part, but if I try to do anything else in the application while sleep is running, it won't let me do anything.  As soon as the interval has played out, then it executes the commands I put in while "Sleep" was running.  

Is there a way of increasing the Timer interval above 64 seconds without locking up the application?

Thanks,
Javaguy78
0
Comment
Question by:JavaGuy78
  • 2
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 50 total points
ID: 9630231
You can use the wonderful function called DateDiff.

Here is a sample app I wrote to show how it works quite nicely.  Create a new project and place three labels and a timer control on it.  The first label will show the current time.  The second label will show total elapsed time since the program started.  The third label will show elapsed time since the last "event".  

This type of scheme is not good for benchmarking apps in the millisecond range, but it works great for longer durations where that kind of accuracy doesn't matter.

The code is pretty self explanatory.  Hope it helps...and here it is:

Dim eventInterval As Long
Dim lastEventTime As Date
Dim initialTime As Date

Private Sub Form_Load()
    ' Set this value to the number of seconds you want inbetween your events
    ' It is a Long, so the max value is...
    '  max val: 2,147,483,647 seconds
    ' or approx 35,791,394 minutes
    ' or approx 596,523 hours
    ' or approx 24,855 days
    ' or approx 68.10 years
    eventInterval = 90
   
    initialTime = Now()
    lastEventTime = initialTime
    Timer1.Interval = 250
End Sub

Private Sub Timer1_Timer()
    Dim curTime As Date
    Dim elapsed As Long
   
    Dim elapsedSeconds As Long
    Dim elapsedHours As Long
    Dim elapsedMinutes As Long
    Dim elapsedString As String
   
    curTime = Now()
   
    ' update current time
    Label1.Caption = Format(curTime, "Long Time")
       
    ' update total elapsed time
    elapsed = DateDiff("s", initialTime, curTime)
    ' conversions to secs, mins and hrs...
    elapsedSeconds = elapsed
    elapsedHours = elapsedSeconds \ 3600
    elapsedSeconds = elapsedSeconds - (elapsedHours * 3600)
    elapsedMinutes = elapsedSeconds \ 60
    elapsedSeconds = elapsedSeconds - (elapsedMinutes * 60)
    elapsedString = Format(elapsedHours, "00") & ":" & _
        Format(elapsedMinutes, "00") & ":" & Format(elapsedSeconds, "00")
    Label2.Caption = elapsedString
   
    ' update elapsed time since last event
    elapsed = DateDiff("s", lastEventTime, curTime)
    ' conversions to secs, mins and hrs...
    elapsedSeconds = elapsed
    elapsedHours = elapsedSeconds \ 3600
    elapsedSeconds = elapsedSeconds - (elapsedHours * 3600)
    elapsedMinutes = elapsedSeconds \ 60
    elapsedSeconds = elapsedSeconds - (elapsedMinutes * 60)
    elapsedString = Format(elapsedHours, "00") & ":" & _
        Format(elapsedMinutes, "00") & ":" & Format(elapsedSeconds, "00")
    Label3.Caption = elapsedString
    If elapsed >= eventInterval Then
        lastEventTime = curTime
        ' place a call to your "event" subroutine here
        '
        ' <-----------------------
        '
        '
    End If
End Sub
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9630241
You could also use the SetTimer and KillTimer API's.
Or the MultiMedia DLL.

Let me know if you would rather go those routes and I can you hellp with code there also.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

746 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

9 Experts available now in Live!

Get 1:1 Help Now