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

Posted on 2004-08-02
Last Modified: 2010-05-18
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.

Question by:pratikshahse

Expert Comment

ID: 11697876

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

Expert Comment

ID: 11697951
Actually 6000 miliseconds is 6 seconds....
for 1 minute you should have 60 ( second ) * 1000 ( miliseconds / second ) = 60000 miliseconds

Accepted Solution

cachedVB earned 25 total points
ID: 11699747
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.


Expert Comment

ID: 11720029

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

Expert Comment

ID: 11729158
I dont beleive you can have interval 60000 because I think the max is 32565

Expert Comment

ID: 11735786

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.

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
wordmultiple challenge 12 131
drawing animated level bar based on numbers 3 96
Currency Conversion? 1 84
How to parse the string and split the sub directory names in 7 38
I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
A short article about problems I had with the new location API and permissions in Marshmallow
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

785 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