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

Comment Utility

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

Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.


Expert Comment

Comment Utility

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

Comment Utility
I dont beleive you can have interval 60000 because I think the max is 32565

Expert Comment

Comment Utility

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

728 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

8 Experts available now in Live!

Get 1:1 Help Now