Help With Initializing "Try, Catch, Wait, Retry"

JNelson

Newcomer
Joined
May 24, 2009
Messages
1
Location
Near Aron Ohio
Here is the beginning of my code. The issue that I have is that this program runs in a CNC Machine. The OS is windows XP and the Machine runs in a real time Operation System. windows launches and then launches the Machines software from the Startup folder in the Start button. it takes a few minutes for the machine to completely launch its software. I need to launch my program from the startup folder also so that the operator does not have to. The problem is that my program will run a lot sooner then the machines OS so when my program tries to initialize the API's it will fail. I have a temp solution to Sleep(120000) on form1_load this helps. But if I load the software and run it with the machines OS already running I have to wait for 2 min before My program runs. I'm trying to figure out how to try to initialize everything if it fails wait for a period of time (about 5sec) then try again and keep doing this until the attempted is made let say 100 times then I want a msgbox that said could not connect and exit out of the program.

Any Ideas will be appreciated
Thanks


Code:
Imports Okuma.CMDATAPI.DataAPI
Public Class Form1
    Inherits System.Windows.Forms.Form
    Private objMachine As CMachine
    Private objVariables As CVariables
    Private objIO As Okuma.CMDATAPI.DataAPI.CIO
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Sleep(120000)
        Try
            objMachine = New CMachine
            objMachine.Init()
            objVariables = New CVariables
            objIO = New CIO
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        ' tmr_CheckDoorState set for 1 second - 1,000 = 1 second
        'This will look for an open door every 1 second
        TimerIntervalUpdate()
        tmr_CheckDoorState.Enabled = True
    End Sub
 
You could put this in a while loop e.g.
Visual Basic:
Imports Okuma.CMDATAPI.DataAPI
Public Class Form1
    Inherits System.Windows.Forms.Form
    Private objMachine As CMachine
    Private objVariables As CVariables
    Private objIO As Okuma.CMDATAPI.DataAPI.CIO
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

dim CMCrunning as boolean = false
while CMCRunning = false
        Try
            objMachine = New CMachine
            objMachine.Init()
            objVariables = New CVariables
            objIO = New CIO
            CMCRunning = true
        Catch ex As Exception
            Sleep(10000)
        End Try
End While
        ' tmr_CheckDoorState set for 1 second - 1,000 = 1 second
        'This will look for an open door every 1 second
        TimerIntervalUpdate()
        tmr_CheckDoorState.Enabled = True
    End Sub
 
Back
Top