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
  • 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?
Reply
  • 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?
Children