how to terminate a program after certain number of minutes have passed?

I have a program which runs automatically every half an hour. Every once in while it is possible that the program would error out as it does not get enough data that is required. in that case the program just stays open and would not run for the next schedule time. If somebody could let me know that how under Visual Basic is possible to terminate the program if it has stayed open for 10 minutes or more.

Who is Participating?
cachedVBConnect With a Mentor Commented:
wouldnt it make more sense to do

Dim Counter As Integer

Private Sub Timer1_Timer()
    Counter = Counter + 1
    If Counter > 100 Then
    End If
End Sub

to terminate the whole application instead of closing that one form

This is quite simple... put a timer on a form. Set the timeout to one minute... that is 6000 miliseconds
Every time the timer hits then update a variable... when the variable hits 10... exit the app...

Dim Counter As Integer

Private Sub Timer1_Timer()
    Counter = Counter + 1
    If Counter > 10 Then
        Unload Me
    End If
End Sub
Actually 6000 miliseconds is 6 seconds....
for 1 minute you should have 60 ( second ) * 1000 ( miliseconds / second ) = 60000 miliseconds
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.


I suggest creating a new program that stays resident and launches the 2nd program every 30 minutes.

This code should work if yoiu create a new project and add a timer "timerRunApplication" with interval 60000 (to preserve cpu). It will also start your program exactly at the same time every hour (at 0 and 30 mins into the hour) and will terminate the application if its still running (hung).

' Function inports
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function CreateProcessA Lib "kernel32" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, ByVal lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDirectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

' Constants

' Structures
        hProcess As Long
        hThread As Long
        dwProcessId As Long
        dwThreadId As Long
End Type
        cb As Long
        lpReserved As String
        lpDesktop As String
        lpTitle As String
        dwX As Long
        dwY As Long
        dwXSize As Long
        dwYSize As Long
        dwXCountChars As Long
        dwYCountChars As Long
        dwFillAttribute As Long
        dwFlags As Long
        wShowWindow As Integer
        cbReserved2 As Integer
        lpReserved2 As Long
        hStdInput As Long
        hStdOutput As Long
        hStdError As Long
End Type

' Form variables
Dim ProcessID As Long

' Timer that runs application every 30 minutes and terminates any allready running ones
Private Sub timerRunApplication_Timer()

   Dim timeNow As Date
   Dim booleanStartProgram As Boolean
   Dim handleProcess As Long

   ' Record time
   timeNow = Time

   ' Is it time yet?
   If Right(Format(timeNow, "hh:mm"), 2) = "30" Then
      booleanStartProgram = True
   End If
   If Right(Format(timeNow, "hh:mm"), 2) = "00" Then
      booleanStartProgram = True
   End If
   ' Quit if not time
   If booleanStartProgram = False Then
      Exit Sub
   End If
   ' Any existing process running?
   If ProcessID <> 0 Then
      ' Open process handle
      handleProcess = OpenProcess(PROCESS_ALL_ACCESS, False, ProcessID)
      ' Kill the process
      TerminateProcess handleProcess, 0
      ' Close handle
      CloseHandle handleProcess
      ' Clear process id
      ProcessID = 0
   End If
   ' Initalize SI
   SI.cb = Len(SI)

   ' Try to start application
   If CreateProcessA("c:\windows\notepad.exe", "", 0, 0, True, 0, ByVal 0&, "c:\windows\", SI, PI) < 1 Then
      MsgBox "Failed to start application."
      Exit Sub
   End If
   ' Record processID
   ProcessID = PI.dwProcessId
End Sub
I dont beleive you can have interval 60000 because I think the max is 32565

Then you either are using Visual Basic 5.0 or have not applied any Servicepacs to Visual Basic 6.0. Latest servicepack for Visual Basic 6.0 is Service Pack 6.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.