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 updated my code to remove Global variables and pass arguments instead. I also added some extra functionality so that it can totally replace all shortcuts for the operator and they can use only my program and I used some code suggested here. I still hit the same road block as before though. The event handler is giving me trouble. I found some code in a repository that does exactly what I need. It works perfect in excel but when I transfer it to visual studio, it only opens PC-Dmis. This is the excel code...

    Dim PCDApp As PCDLRN.Application
    Dim WithEvents AppEvents As PCDLRN.ApplicationObjectEvents
    Sub Start()
    HideExcel
    End Sub
    Private Sub HideExcel()
    Dim intAnswer As Integer
    intAnswer = MsgBox("Do you want to make Excel invisible? For this test, you should click Yes. It will become visible when you open a part program.", vbYesNo, "Hide Excel?")
    
    If intAnswer = vbYes Then
    Application.Visible = False
    Else
    Application.Visible = True
    End If
    
    LaunchPCDMIS
    
    End Sub
    Sub LaunchPCDMIS()
    Set PCDApp = CreateObject("PCDLRN.Application")
    Set AppEvents = PCDApp.ApplicationEvents
    PCDApp.Visible = True
    End Sub
    Private Sub AppEvents_OnOpenPartProgram(ByVal PartProg As PCDLRN.IPartProgram) [COLOR=#e74c3c]Hangles AppEvents.OnOpenPartProgram[/COLOR]
    ' Event subroutine. This activates when you OPEN a part program.
    Set PartProg = PCDApp.ActivePartProgram
    Application.Visible = True
    MsgBox "Part Program " & PartProg.Name & " opened. Excel should also be visible."
    End Sub
    Private Sub AppEvents_OnStartExecution(ByVal PartProg As PCDLRN.IPartProgram) [COLOR=#e74c3c]Handles AppEvents.OnStartExecution[/COLOR]
    ' Event subroutine. This activates when you START EXECUTION of the part program.
    MsgBox "STARTING EXECUTION OF " & PartProg.Name & ". Click OK to continue."
    End Sub
    Private Sub AppEvents_OnEndExecution(ByVal PartProg As PCDLRN.IPartProgram, ByVal TerminationType As Long) [COLOR=#e74c3c]Handles AppEvents.OnEndExecution[/COLOR]
    ' Event subroutine. This activates when you END EXECUTION of the part program.
    MsgBox "ENDING EXECUTION OF " & PartProg.Name & ". Click OK to continue."
    End Sub


    I've tried to remove the excel portion of this and launch LaunchPCDMIS through a button and it doesn't work. Does anyone have any advice on how to update this code to be current? FYI, I removed the "Set" before every variable and surrounded the MsgBox with ()
    My end goal here is when PC-Dmis starts running a program it will disable the buttons on my form so someone can't open something over top the running program and when the execution ends it enables the buttons. If I can only get the event handler to work I could get the rest done.
    Thanks again for your help guys


    Try this buddy.... on your Visual Studio VB.NET application.

    Here's my test...

    Imports PCDLRN
    Public Class Form1
    ' Globals
    Dim oPcd As Application = New Application()
    Dim WithEvents AppEvent As ApplicationObjectEvents
    
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim oPart As PartProgram = oPcd.ActivePartProgram
    AppEvent = oPcd.ApplicationEvents
    End Sub
    
    Private Sub AppEvents_OnOpenPartProgram(ByVal partProg As PartProgram) [COLOR=#e74c3c]Handles AppEvent.OnOpenPartProgram[/COLOR]
    partProg = oPcd.ActivePartProgram
    MsgBox("Part: " & partProg.Name & " opened.")
    End Sub
    End Class
    
    
    


    I even found this little guy: https://www.pcdmisforum.com/forum/pc...ual-basic-2008
  • 2019 which I believe is the latest version.
  • That makes complete sense! Going off of the code needed to activate button clicks...

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    All of the eventargs have it, so I think you fixed my problem! I will test this tonight but I'm confident this fixes it. If I could like on your post 5x I would right now.
  • Awesome guys! Thanks for the help. Once the code is completely finished, I will post it for anyone to edit or use to their liking. After testing what I have, I noticed that opening Operator Mode only halfway works. It looks like operator but you can still access the edit window if you go through the menu View-Edit Window. IF PC-Dmis is already open, this doesn't happen. If I can't figure out a solid solution for this, I will just change my open code to open the .lnk shortcut used to open operator mode and watch task manager for when it starts. Not ideal but in theory it should work.
  • It looks like operator but you can still access the edit window if you go through the menu View-Edit Window. IF PC-Dmis is already open, this doesn't happen.


    I have a general warning for folks starting a pc-dmis on-line session using CreateObject (or any method I tried). Things DO NOT work right. I believe it's because our code is getting ahead of the pcd initialization process. What I observed is that very important pc-dmis messages were being suppressed. None of these messages were displayed:

    - "Select Probe File" (when pcd is launched)
    -"Program is using a probe that is not defined" (when program is opened)
    -" Program is using a tip that is not calibrated! (when program opened)

    You'll also find that when pc-dmis is exited, it will still be running in the task manager process list, and it won't respond to Application.Quit in your code. Kill it with task manager, and any settings changes made during the pcd session are lost. It's a mess.

    The key is to wait until pcd is ready. The question is, "when is that?"

    Application.WaitUntilReady() is a joke. Doesn't wait for anything far as I can tell. I beat my head against the wall trying to figure this out. Different machine interfaces and computers are ready at differing intervals. Finally decided to try waiting for a pc-dmis window to appear. Initially tried waiting for the "Select Probe File" window, which worked on all machines but two that use DME interface. Eventually settled on waiting for the pc-dmis Open window. Then I execute the CreateObject() command. We implement standard settings across all machines, one of which is to always show the pcd Open dialog at startup. We do this with batch files/registry import files.

    In our start pcd batch files, the event handler .exe is started before pc-dmis, and sits waiting for the pc-dmis Open dialog to appear.

    I use FindWindowEx from the Windows API (user32.dll) in a loop with Thread.Sleep() to wait for the window to appear.

    I use a Timer where the Timer.Elapsed event handler looks for pcd to become invisible(every 10 seconds), which is my cue that the user is trying to close pcd, and I need to close the event handler. I've found that the vb "End" command closes pcd gracefully, solving the problem above.

    Wish I could post my code...


  • This was my "Fix"

    Private Sub File_Explorer_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim varSourceopen As String = "Opened"
            If Process.GetProcessesByName("PCDLRN").Length = 0 Then
                Process.Start("C:\Program Files\Hexagon\PC-DMIS 2017 R2 64-bit\PCDLRN.exe", "/o")
            End If
    
            Do
                If Process.GetProcessesByName("PCDLRN").Length > 0 Then
                    PCDApp = CreateObject("PCDLRN.Application")
                    AppEvent = PCDApp.ApplicationEvents
                    PCDApp.OperatorMode = True
                    Exit Do
                Else
                    Threading.Thread.Sleep(1000)
                End If
            Loop
    
            PCD_Create(varSourceopen)
    
        End Sub


    It's basically opening Operator mode through the ,exe and passing the operator parameter. Then it waits until the app shows up in tasks then attaches. Seems to work good so far. But now I'm running into another issue. I'm trying to disable 2 buttons upon the start of a program to ensure another program will not be opened while another is running.

    Private Sub AppEvents_OnStartExecution(ByVal PartProg As PartProgram) Handles AppEvent.OnStartExecution
            ' Event subroutine. This activates when you START EXECUTION of the part program.
            Button1.Enabled = False
            Button2.Enabled = False
            Me.Close()
        End Sub


    No matter what I tried, when the event is triggered only one line is read and the rest are skipped. I can put the same code in a button and it works fine. I tried having it show 3 message boxes after the event triggered but the first one shows, then once I hit ok I get a server busy error message. If anyone else uses onstartexecution, have they seen the same thing?
    Once I get this button things figured out I think I will be good to go
  • I added this to my program and I'm turning off 2 buttons. Both start and end event handlers only read the first line and ignores the rest. Basically, only disabling and enabling the first button. Only reason why I want to disable them is so someone doesn't open another program when one is running.
  • This seems to be a problem on my end. I tested code by itself with several lines of code and it all worked. I will start from the beginning and troubleshoot.
  • I just tested the code by itself without anything else and it worked perfect. I will go back to the drawing board and updated everyone when it is finished.
  • You could disable the buttons after you click them (on_click? I'm not sure the correct syntax for vb) and refresh the form when execution completes. I could post my c# example if it helps you. I was also getting the server busy error so I'd be interested if someone has a fix for that error, the only workaround that i found is to break up the code into smaller pieces and it seems to be working correctly