Solved

Pause/Wait without Locking up application

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

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

Suggested Solutions

Title # Comments Views Activity
How to delete rows after an End (xl Down) without deleting header row? 3 70
Validating VB6 Function 19 56
Adding to a VBA? 6 62
Modifying Conditional Format from VBA code 3 49
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

895 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

17 Experts available now in Live!

Get 1:1 Help Now