hexagon logo

VB code/ If a program is running

Does anyone know what code would be used to find out if a program is currently running in PC-Dmis? I wrote some code to automate the execution process and if I use it while a program is running, it closes the program and opens the new program while the original program continues to run. (Not sure for how long though. I stopped it after I tested it.)
Parents
  • This is all tied to a form I created in Visual Studio. Right now, I am focusing on making is so this code will not work if a program is currently running. I have something I planned on trying today, which will require some googling, but I could have it done before tomorrow.

    1) I figure I will need this form to always be open for the start and end execution event to trigger. I plan on changing the end of each button click so it doesn't close, but instead minimizes to the system tray.
    2) I will assign a variable with a value of "Stopped" at the beginning of the class. Then I'll create a start and end event trigger sub that will change the value of this variable to "Started" or "Stopped" depending on the trigger.
    3) The buttons would use the previous variable to only execute if the value is set to "Stopped" Which I'm assuming would render the buttons useless until the trigger changes the variable.
    4) Lastly, I would like the .exe of the form to maximize the form from the system tray instead of opening another instance. I found the setting inside of Visual Studio that locks it to only one instance, but I haven't testing if double clicking the .exe will automatically maximize from the system tray or if that is something I need to program in.

    I think if I do the above 4 things, this will be almost bug free. The only other bug I found is if the open dialog box is showing in PC-Dmis when I use this, the dialog box stays open when the script opens the program. (Which the operator can just hit cancel.) But would be nice to automatically hide that window if it is open.

    You guys are awesome for helping with this.

    Public Class File_Explorer
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim PCDApp, PCDPartProgram, PCDPartPrograms
    
    PCDApp = CreateObject("PCDLRN.Application")
    PCDPartPrograms = PCDApp.PartPrograms
    'If PC-DMIS is open, close current program
    If PCDApp.Visible = True Then
    PCDPartProgram = PCDApp.ActivePartProgram
    On Error Resume Next
    PCDPartProgram.Close()
    End If
    'If PC-DMIS is not open, open operator mode
    If PCDApp.Visible = False Then
    PCDApp.OperatorMode = True
    PCDApp.Visible = True
    End If
    
    OpenFileDialog1.InitialDirectory = "ORIGINAL FILE DIRECTORY HERE"
    OpenFileDialog1.Title = "Find your Program"
    OpenFileDialog1.Filter = ".PRG files|*.prg"
    OpenFileDialog1.FileName = ""
    OpenFileDialog1.Multiselect = False
    If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
    Dim sourceDir As String = OpenFileDialog1.FileName 'Sets 'sourceDir' to the location of the selected PRG file
    Dim sourceFileName As String = Dir(sourceDir) 'Extracts PRG file name from path
    Dim cadDir As String = IO.Path.ChangeExtension(sourceDir, ".CAD") 'Sets 'cadDir' to the location of the selected CAD file
    Dim cadFileName As String = Dir(cadDir) 'Extracts CAD file name from path
    
    Dim destDir As String = IO.Path.Combine("C:\CMM\", sourceFileName) 'Get PRG Destination Path
    Dim destCADDir As String = IO.Path.Combine("C:\CMM\", cadFileName) 'Get CAD Destination Path
    'If the CAD file doesn't already exist in new directory, then copy the file
    If Not My.Computer.FileSystem.FileExists(destCADDir) Then
    'Copy file
    IO.File.Copy(cadDir, destCADDir)
    End If
    'If the PRG file doesn't already exist in new directory, then copy the file
    If Not My.Computer.FileSystem.FileExists(destDir) Then
    'Copy file
    IO.File.Copy(sourceDir, destDir)
    End If
    'If PC-DMIS is not open, open operator mode
    If PCDApp.Visible = False Then
    PCDApp.OperatorMode = True
    PCDApp.Visible = True
    End If
    'Check if both PRG and CAD files exists. If they do, open the PRG. If not, wait 1 sec and loop
    Do
    If My.Computer.FileSystem.FileExists(destDir) And My.Computer.FileSystem.FileExists(destCADDir) Then
    PCDPartPrograms.Open(destDir, "CMM1")
    Exit Do
    Else
    Threading.Thread.Sleep(1000)
    End If
    Loop
    'Closes the form
    Me.Close()
    
    End If
    
    End Sub
    
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim PCDApp, PCDPartProgram, PCDPartPrograms
    
    PCDApp = CreateObject("PCDLRN.Application")
    PCDPartPrograms = PCDApp.PartPrograms
    'If PC-DMIS is open, close current program
    If PCDApp.Visible = True Then
    PCDPartProgram = PCDApp.ActivePartProgram
    On Error Resume Next
    PCDPartProgram.Close()
    End If
    'If PC-DMIS is not open, open operator mode
    If PCDApp.Visible = False Then
    PCDApp.OperatorMode = True
    PCDApp.Visible = True
    End If
    
    OpenFileDialog1.InitialDirectory = "ORIGINAL FILE DIRECTORY HERE"
    OpenFileDialog1.Title = "Find your Program"
    OpenFileDialog1.Filter = ".PRG files|*.prg"
    OpenFileDialog1.FileName = ""
    OpenFileDialog1.Multiselect = False
    If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
    Dim sourceDir As String = OpenFileDialog1.FileName 'Sets 'sourceDir' to the location of the selected PRG file
    Dim sourceFileName As String = Dir(sourceDir) 'Extracts PRG file name from path
    Dim cadDir As String = IO.Path.ChangeExtension(sourceDir, ".CAD") 'Sets 'cadDir' to the location of the selected CAD file
    Dim cadFileName As String = Dir(cadDir) 'Extracts CAD file name from path
    
    Dim destDir As String = IO.Path.Combine("C:\CMM\", sourceFileName) 'Get PRG Destination Path
    Dim destCADDir As String = IO.Path.Combine("C:\CMM\", cadFileName) 'Get CAD Destination Path
    'If the CAD file doesn't already exist in new directory, then copy the file
    If Not My.Computer.FileSystem.FileExists(destCADDir) Then
    'Copy file
    IO.File.Copy(cadDir, destCADDir)
    End If
    'If the PRG file doesn't already exist in new directory, then copy the file
    If Not My.Computer.FileSystem.FileExists(destDir) Then
    'Copy file
    IO.File.Copy(sourceDir, destDir)
    End If
    'If PC-DMIS is not open, open operator mode
    If PCDApp.Visible = False Then
    PCDApp.OperatorMode = True
    PCDApp.Visible = True
    End If
    'Check if both PRG and CAD files exists. If they do, open the PRG. If not, wait 1 sec and loop
    Do
    If My.Computer.FileSystem.FileExists(destDir) And My.Computer.FileSystem.FileExists(destCADDir) Then
    PCDPartPrograms.Open(destDir, "CMM1")
    Exit Do
    Else
    Threading.Thread.Sleep(1000)
    End If
    Loop
    'Closes the form
    Me.Close()
    
    End If
    
    End Sub
    
    End Class
Reply
  • This is all tied to a form I created in Visual Studio. Right now, I am focusing on making is so this code will not work if a program is currently running. I have something I planned on trying today, which will require some googling, but I could have it done before tomorrow.

    1) I figure I will need this form to always be open for the start and end execution event to trigger. I plan on changing the end of each button click so it doesn't close, but instead minimizes to the system tray.
    2) I will assign a variable with a value of "Stopped" at the beginning of the class. Then I'll create a start and end event trigger sub that will change the value of this variable to "Started" or "Stopped" depending on the trigger.
    3) The buttons would use the previous variable to only execute if the value is set to "Stopped" Which I'm assuming would render the buttons useless until the trigger changes the variable.
    4) Lastly, I would like the .exe of the form to maximize the form from the system tray instead of opening another instance. I found the setting inside of Visual Studio that locks it to only one instance, but I haven't testing if double clicking the .exe will automatically maximize from the system tray or if that is something I need to program in.

    I think if I do the above 4 things, this will be almost bug free. The only other bug I found is if the open dialog box is showing in PC-Dmis when I use this, the dialog box stays open when the script opens the program. (Which the operator can just hit cancel.) But would be nice to automatically hide that window if it is open.

    You guys are awesome for helping with this.

    Public Class File_Explorer
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim PCDApp, PCDPartProgram, PCDPartPrograms
    
    PCDApp = CreateObject("PCDLRN.Application")
    PCDPartPrograms = PCDApp.PartPrograms
    'If PC-DMIS is open, close current program
    If PCDApp.Visible = True Then
    PCDPartProgram = PCDApp.ActivePartProgram
    On Error Resume Next
    PCDPartProgram.Close()
    End If
    'If PC-DMIS is not open, open operator mode
    If PCDApp.Visible = False Then
    PCDApp.OperatorMode = True
    PCDApp.Visible = True
    End If
    
    OpenFileDialog1.InitialDirectory = "ORIGINAL FILE DIRECTORY HERE"
    OpenFileDialog1.Title = "Find your Program"
    OpenFileDialog1.Filter = ".PRG files|*.prg"
    OpenFileDialog1.FileName = ""
    OpenFileDialog1.Multiselect = False
    If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
    Dim sourceDir As String = OpenFileDialog1.FileName 'Sets 'sourceDir' to the location of the selected PRG file
    Dim sourceFileName As String = Dir(sourceDir) 'Extracts PRG file name from path
    Dim cadDir As String = IO.Path.ChangeExtension(sourceDir, ".CAD") 'Sets 'cadDir' to the location of the selected CAD file
    Dim cadFileName As String = Dir(cadDir) 'Extracts CAD file name from path
    
    Dim destDir As String = IO.Path.Combine("C:\CMM\", sourceFileName) 'Get PRG Destination Path
    Dim destCADDir As String = IO.Path.Combine("C:\CMM\", cadFileName) 'Get CAD Destination Path
    'If the CAD file doesn't already exist in new directory, then copy the file
    If Not My.Computer.FileSystem.FileExists(destCADDir) Then
    'Copy file
    IO.File.Copy(cadDir, destCADDir)
    End If
    'If the PRG file doesn't already exist in new directory, then copy the file
    If Not My.Computer.FileSystem.FileExists(destDir) Then
    'Copy file
    IO.File.Copy(sourceDir, destDir)
    End If
    'If PC-DMIS is not open, open operator mode
    If PCDApp.Visible = False Then
    PCDApp.OperatorMode = True
    PCDApp.Visible = True
    End If
    'Check if both PRG and CAD files exists. If they do, open the PRG. If not, wait 1 sec and loop
    Do
    If My.Computer.FileSystem.FileExists(destDir) And My.Computer.FileSystem.FileExists(destCADDir) Then
    PCDPartPrograms.Open(destDir, "CMM1")
    Exit Do
    Else
    Threading.Thread.Sleep(1000)
    End If
    Loop
    'Closes the form
    Me.Close()
    
    End If
    
    End Sub
    
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim PCDApp, PCDPartProgram, PCDPartPrograms
    
    PCDApp = CreateObject("PCDLRN.Application")
    PCDPartPrograms = PCDApp.PartPrograms
    'If PC-DMIS is open, close current program
    If PCDApp.Visible = True Then
    PCDPartProgram = PCDApp.ActivePartProgram
    On Error Resume Next
    PCDPartProgram.Close()
    End If
    'If PC-DMIS is not open, open operator mode
    If PCDApp.Visible = False Then
    PCDApp.OperatorMode = True
    PCDApp.Visible = True
    End If
    
    OpenFileDialog1.InitialDirectory = "ORIGINAL FILE DIRECTORY HERE"
    OpenFileDialog1.Title = "Find your Program"
    OpenFileDialog1.Filter = ".PRG files|*.prg"
    OpenFileDialog1.FileName = ""
    OpenFileDialog1.Multiselect = False
    If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
    Dim sourceDir As String = OpenFileDialog1.FileName 'Sets 'sourceDir' to the location of the selected PRG file
    Dim sourceFileName As String = Dir(sourceDir) 'Extracts PRG file name from path
    Dim cadDir As String = IO.Path.ChangeExtension(sourceDir, ".CAD") 'Sets 'cadDir' to the location of the selected CAD file
    Dim cadFileName As String = Dir(cadDir) 'Extracts CAD file name from path
    
    Dim destDir As String = IO.Path.Combine("C:\CMM\", sourceFileName) 'Get PRG Destination Path
    Dim destCADDir As String = IO.Path.Combine("C:\CMM\", cadFileName) 'Get CAD Destination Path
    'If the CAD file doesn't already exist in new directory, then copy the file
    If Not My.Computer.FileSystem.FileExists(destCADDir) Then
    'Copy file
    IO.File.Copy(cadDir, destCADDir)
    End If
    'If the PRG file doesn't already exist in new directory, then copy the file
    If Not My.Computer.FileSystem.FileExists(destDir) Then
    'Copy file
    IO.File.Copy(sourceDir, destDir)
    End If
    'If PC-DMIS is not open, open operator mode
    If PCDApp.Visible = False Then
    PCDApp.OperatorMode = True
    PCDApp.Visible = True
    End If
    'Check if both PRG and CAD files exists. If they do, open the PRG. If not, wait 1 sec and loop
    Do
    If My.Computer.FileSystem.FileExists(destDir) And My.Computer.FileSystem.FileExists(destCADDir) Then
    PCDPartPrograms.Open(destDir, "CMM1")
    Exit Do
    Else
    Threading.Thread.Sleep(1000)
    End If
    Loop
    'Closes the form
    Me.Close()
    
    End If
    
    End Sub
    
    End Class
Children
No Data