Solved

Pause/Wait without Locking up application

Posted on 2003-10-27
2
365 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

792 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