Search code examples
registrywindows-installerstartupokuma

Auto Start Application on Startup - Wait for API to be available


Alternate title: Auto-run THINC app when control is turned on (run only after OSP NC system started)

I am writing an application for the OSP-P300 control (Running WinXP) and want it to start automatically when the control boots up / turns on. I have tried using a shortcut in the startup folder but that is causing an issue.

When the app runs before the NC software is finished starting, I get errors from my THINC API functions. (The API is not available yet)

I know about the "Okuma THINC Startup Service" program, and have it running on my control. When manually configured, this method takes care of the issue and loads my app at the appropriate time.

My question is: Is there a way to add my application to the Startup Service programmatically, during the install process?

Yes, the user can still do this manually, but a check-box option during install that is checked by default would be SO much simpler.

Could it be as simple as adding a few registry keys?


Solution

  • Use the startup service that comes on the API disk.
    Here is the class I use to register/unregister.
    The CReg class I got from here : RegistryKeyAccess.vb

    Imports Microsoft.Win32

    Public Class ThincStartupReg
    
        Public Overloads Shared Sub Register(ApplicationPath As String, ApplicationName As String, AppType As enumAppType, wait As Boolean, LaunchType As enumLaunch)
            Try
                Dim ObjReg As New CReg
                Dim regCreated As Boolean
    
                If ObjReg.ReadValue(ObjReg.HKeyLocalMachine, "SOFTWARE\OAC\StartupService\", "State") Then
    
                    'Startup Service is installed
    
                    If Not ObjReg.ReadValue(ObjReg.HKeyLocalMachine, "SOFTWARE\OAC\Startup\" & ApplicationName, "Enabled") Then
                        'No entry for this program
                        regCreated = ObjReg.CreateSubKey(ObjReg.HKeyLocalMachine, "SOFTWARE\OAC\Startup\" & ApplicationName)
    
                    Else
                        regCreated = True
                    End If
    
                    If regCreated Then
                        ObjReg.WriteValue(ObjReg.HKeyLocalMachine, "SOFTWARE\OAC\Startup\" & ApplicationName,
                                         "Type", "Process")
                        ObjReg.WriteValue(ObjReg.HKeyLocalMachine, "SOFTWARE\OAC\Startup\" & ApplicationName,
                                          "Name", ApplicationName)
                        ObjReg.WriteValue(ObjReg.HKeyLocalMachine, "SOFTWARE\OAC\Startup\" & ApplicationName,
                                          "Enabled", "True")
                        ObjReg.WriteValue(ObjReg.HKeyLocalMachine, "SOFTWARE\OAC\Startup\" & ApplicationName,
                                          "Wait", If(wait, "True", "False"))
                        ObjReg.WriteValue(ObjReg.HKeyLocalMachine, "SOFTWARE\OAC\Startup\" & ApplicationName,
                                          "Type", If(AppType = enumAppType.Process, "Process", "Service"))
                        ObjReg.WriteValue(ObjReg.HKeyLocalMachine, "SOFTWARE\OAC\Startup\" & ApplicationName,
                                          "Launch", If(LaunchType = enumLaunch.LaunchOnce, "Once", "Monitor"))
                        ObjReg.WriteValue(ObjReg.HKeyLocalMachine, "SOFTWARE\OAC\Startup\" & ApplicationName,
                                         "File", ApplicationPath)
                    End If
    
    
                End If
    
            Catch ex As Exception
                Throw ex
            End Try
    
        End Sub
    
        Public Overloads Shared Sub Register(ThisAssembly As System.Reflection.Assembly, ByVal AppType As enumAppType, ByVal Wait As Boolean, ByVal LaunchType As enumLaunch)
            Dim AppName = ThisAssembly.FullName.Split(",")(0)
            Dim AppPath = ThisAssembly.Location
            Register(AppPath, AppName, AppType, Wait, LaunchType)
        End Sub
    
        Public Shared Sub UnRegister()
            Try
                Dim ObjReg As New CReg
                Dim AppName = System.Reflection.Assembly.GetExecutingAssembly().FullName.Split(",")(0)
    
                If ObjReg.ReadValue(ObjReg.HKeyLocalMachine, "SOFTWARE\OAC\StartupService\", "State") Then
                    If ObjReg.ReadValue(ObjReg.HKeyLocalMachine, "SOFTWARE\OAC\Startup\" & AppName, "Enabled") Then
                        ObjReg.DeleteSubKey(ObjReg.HKeyLocalMachine, "SOFTWARE\OAC\Startup\" & AppName)
                    End If
    
                End If
    
            Catch ex As Exception
                Throw ex
            End Try
    
        End Sub
    
    
    End Class
    

    If you're not sure the install service will be installed you could just add your shortcut to the startup folder and then just loop until the OSP process has started.

    Public Shared Function Wait(Timeout As TimeSpan) As Integer
            If File.Exists("C:\OSP-P\OSPMNGCD.CNC") Then
                Dim startTime = Now
                Dim myProcess As Process() = Process.GetProcessesByName("PNC-P200")
                While myProcess.Length = 0
                    If Now.Subtract(startTime) >= Timeout Then Return -1
                    myProcess = Process.GetProcessesByName("PNC-P200")
                    Thread.Sleep(1000)
                End While
                'OSP Started
                Return 1
            End If
            'Simulation mode (not on a machine)
            Return 2
        End Function