Solved

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

Posted on 2004-08-02
6
610 Views
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.

0
Comment
Question by:pratikshahse
6 Comments
 
LVL 4

Expert Comment

by:Sicos
ID: 11697876
Hi,

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
0
 

Expert Comment

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

Accepted Solution

by:
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
    End If
End Sub

to terminate the whole application instead of closing that one form
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:KJHDI12
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
Private Const PROCESS_ALL_ACCESS = &H1F0FFF

' Structures
Private Type PROCESS_INFORMATION
        hProcess As Long
        hThread As Long
        dwProcessId As Long
        dwThreadId As Long
End Type
Private Type STARTUPINFO
        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 SI As STARTUPINFO
   Dim PI As PROCESS_INFORMATION
   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
0
 
LVL 4

Expert Comment

by:cachedVB
ID: 11729158
I dont beleive you can have interval 60000 because I think the max is 32565
0
 
LVL 2

Expert Comment

by:KJHDI12
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.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 125
Basic Java Case or If-Else statement... 3 64
VB.NET String Settings and Temp Folder Question 3 68
batch file or script 4 47
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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 fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

740 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