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.)
  • I'm not sure if there is a specific command to get what you're looking for, but I use "ApplicationObjectEvents.OnStartExecution" and set a bool variable = true at the beginning of the program and then set it to false using "ApplicationObjectEvents.OnEndExecution" at the end.

    Here's a link to object library: https://docs.hexagonmi.com/pcdmis/20...s_members.html

    HTH
  • I apologize. I'm fairly new to visual basic. Basically, you use those 2 objects to set variables inside of the VB script?
    Could I, for example, write a line in VB that pulls the active program and set a variable to this program, then use that variable for these 2 object events so it knows which program to look for automatically? For this to happen, the script would also have to continuously run, correct? Do you have an example code I could use for reference on this? (Still learning.) I appreciate the help on this.
  • Will the OnEndExecution work during an alarm or an unintended end to a program? Or does the program have to complete fully?
  • Post your code. You can attach to a running process without closing first.
  • The program would have to complete fully i believe for that to update. I use the "OnCancelExecution" and other application events to track the behavior during execution
  • If you post your code we can probably get you pointed in the right direction
  • 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
  • Played with it a little but have to get back to something else... hope it's enough to get you unstuck.

    ' Globals
    Private busy As Boolean = False
    Private complete As Boolean = True
    Private PCDApp As PCDLRN.Application = Nothing ' take advantage of intellisence by using this method of instantiating.
    Private PCDPartPrograms As PCDLRN.PartPrograms
    Private PCDPartProgram As PCDLRN.PartProgram = Nothing
    Private destDir As String = ""
    Dim destCADDir As String = ""
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	'Dim PCDApp, PCDPartProgram, PCDPartPrograms	
    
    	PCDApp = CreateObject("PCDLRN.Application") '<-- if Pcdmis is not running this will start it, if open then it'll only attach to it.
    	PCDPartPrograms = PCDApp.PartPrograms
    
    	'If PC-DMIS is open, close current program
    	If PCDApp.Visible = True Then ' <-- if Pcdmis is NOT hidden then
    		PCDPartProgram = PCDApp.ActivePartProgram ' <-- attach exe to currently opened part program
    		'On Error Resume Next ' <-- it'll make you a better dev if you avoid these.
    		'PCDPartProgram.Close() ' you're closing current part program...why?
    	End If
    
    	'If PC-DMIS is not open, open operator mode
    	If PCDApp.Visible = False Then ' <-- if Pcdmis IS hidden then
    		PCDApp.OperatorMode = True ' <-- switching modes (I've never tested this, so no input to give)
    		PCDApp.Visible = True ' <-- making Pcdmis visible if hidden
    	End If
    
    	Dim OpenFileDialog1 As New OpenFileDialog()
    
    	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
    
    		destCADDir = IO.Path.Combine("C:\CMM\", cadFileName) 'Get CAD Destination Path
    		destDir = IO.Path.Combine("C:\CMM\", sourceFileName) 'Get PRG Destination Path
    
    		Dim t As Threading.Thread
    
    		If Not busy Then
    			t = New Threading.Thread(AddressOf MyThread)
    		End If
    
    		'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
    
    		' Start FileIO thread
    		If Not busy Then
    			busy = True
    			t.Start()
    		End If
    
    
    		'Closes the form
    		'Me.Close() ' you want to hide maybe?
    		'Me.Hide()
    	End If
    
    End Sub
    
    Private Sub MyThread()
    	If busy And complete Then
    		complete = False
    		'Check if both PRG and CAD files exists. If they do, open the PRG. If not, wait 1 sec and loop
    		While busy
    			If My.Computer.FileSystem.FileExists(destDir) And My.Computer.FileSystem.FileExists(destCADDir) Then
    				PCDPartPrograms.Open(destDir, "CMM1")
    				busy = False
    				complete = True
    				'Me.Show() ' causes a crss thread error FYI
    				Exit While
    			Else
    				Threading.Thread.Sleep(1000)
    			End If
    		End While
    	End If
    End Sub
    


    hate that it loses indentation when pasted on here!

    PS why not extract directory paths from selected files? That might, also, be what's causing your app to open the 'wrong' file, maybe?
  • I will dig through this when I get home and do some testing. What I had written was pulling the file path and name from the selected file and it was copying and opening the correct program. I just wanted to disable the opening of a new program is the CMM was running a program. I tested out hiding and minimizing but double clicking on the .exe didnt open the form again so ever time I tested my code I had to end in task manager :-/
  • Thank you! I will test this out tonight and let you know how it goes. I tried to get my code to trigger when a program started running and basically spent a few hours failing.