Solved

VB Timer to close/exit program is Function or Procedure is not completed within 3 minutes

Posted on 2004-08-30
4
343 Views
Last Modified: 2008-03-17
I am writing source to launch a HTLM page program in VB.  The HTLM program will prompt the user for an ID and Password.  I found a bug in my program of the user does not enter in the correct ID and Password, SO I would like to put in a timer within the procedure that enters the ID and Password, and if the procedure is not executed within 3 minutes, I will have the program go to my sub routine to close the program and forms that were launched from the program.

Here is my main routine:

Sub Main()

If checkScriptShutdown Then Exit Sub



'Debug.Print "Main"
'--------------------------------------------------------------------
'On Error GoTo ErrHandler

'appShut = False ' We will check this periodically to see if we should end the script

' -------------------------Launch the status screens/forms--------------------------------

myApp.initializeCcowScript


Dim Launchstatus As Long


cmdline = Command

    AppUserId = GetUserID(cmdline)
    '***********************************
    '*   Dashboard Logoff procedure    *
    '***********************************
    'If AppUserId = "shutdown" Then
    '    CloseProgram
    'End If
    AppPAssword = getPassword(cmdline)

'AppUserId = ""
'AppPAssword = ""



Load CcowStatusForm
CcowStatusForm.Show vbModeless


retcode = myApp.Join(False) ' Defined in Ccow.ScriptModule

If (retcode <> 0) Then
    MsgBox (myApp.getLastErrorText)
    myApp.Leave
    Unload CcowStatusForm
    myApp.terminateCcowScript
    Exit Sub
End If
'------------------------------- Get User ID and Password -------------------
'**************************************************
'*   Enter in ID and Password in the program      *
'**************************************************
'-------------------------------- Launch WEB1000 -----------------------------------------
launchApp

'------------------------------- Check to see if Context Manager has a MRN -----------------
main_WEB1000


The problem is that launchApp will take place irregardless if the user has entered in a correct ID and Password.  LaunchApp starts a Java applications and so I cannot scrape the screen for messages.  I am trying to figure out if placing a timer in the program will help.

Here is my close routine


End Sub
Sub CloseProgram()
'Debug.Print "CloseProgram"
'--------------------------------------------------------------------
Wait 0.5
Activate "Agfa"
Key "%fc"
myApp.Leave
Unload CcowStatusForm
myApp.terminateCcowScript
End


How do you set up a timer call in this sub routine?

Thank you for your time

Ray
End Sub
0
Comment
Question by:tsoodo
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
mladenovicz earned 300 total points
ID: 11930093
Put timer control on form and set Interval property on 60000. Also, Enabled should be True. add this code

Private Sub Timer1_Timer()
Static FiredNum As Integer
    FiredNum = FiredNum + 1
   
    If FiredNum = 3 Then
        '3 minutes expired
        'do sthg
    End If
End Sub

In function that should be executed within 3 minutes, add this code (at the end)
Timer1.Enabled = False ' this disables timer
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 200 total points
ID: 11930960
mladenovicz's code should work great.  I just thought I'd throw out a snippet of code in case you need it.  It demonstrates how to produce a timeout loop that keeps the code in the same subroutine.  If you don't need the code to hold in the same sub/function then a timer is a bette way to go about it.

Regards,

Idle_Mind

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub Command1_Click()
    Private targetTime As Date
     
    targetTime = DateAdd("n", 3, Now()) ' compute 3 mintues from now
    While Now <= targetTime ' stay in loop until target time is reached...
        Sleep 100 ' keeps CPU Usage down
        DoEvents ' keep the app responsive during timeout loop
    Wend
   
    ' continue with your code...
End Sub
0
 

Author Comment

by:tsoodo
ID: 11932971
Thank you both for the comments.  I am sorry that I did not get this sooner.

Question, my VB code does not have a form running and the launchApp function that is in the Sub Main() routine will always run irregardless of wether or not the user puts in the correct ID and Password or not.

My confusion is that when the http application launches, would it be better to put the time parameter in this function? or in the
Sub Main routine?
(Note: I am sure you can see I am not a very experienced VB programmer.  If this was cobol of assembler I would be ok ;-)

What is goofing me up is that the message screen from the application on stating that the user ID and Password is not correct and I am not sure if I stay in this routine or the Sub Main routine?

Here is my code for Launch App
Function launchApp() As Long
'Debug.Print "launchApp"
'--------------------------------------------------------------------
On Error GoTo scripterror

'MsgBox ("Id=" + AppUserID + vbCrLf + "Password=" + AppPAssword)

Shell "iexplore.exe http://??????"


Wait 4
Do
Wait 1
Activate "Agfa"
Loop Until Err = 0
Connect "Agfa", stWindows
'h = R.hwnd
Wait 4
'====================
'Tab_
Key AppUserId
Tab_
Enter AppPAssword
    'Tab_ ""
    'Enter ""
   
Do
Activate "application"
'Connect "application", stWindows
Wait 1
Loop Until Err = 0
Connect "application", stWindows
Set t.W = R
Key "@_LOGON"
web1000Hwnd = Found("application")
Exit Function
scripterror:
If Err.Number <> 0 Then DoEvents: Resume


End Function

Again - thank you for your help!

tsoodo
0
 

Author Comment

by:tsoodo
ID: 11953250
Thank you both for your help!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

706 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

21 Experts available now in Live!

Get 1:1 Help Now