Solved

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

Posted on 2004-08-02
6
612 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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 …
Progress

634 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