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.)
  • So clicking on source will actually retain indentation when you paste on here. Much better on the eyes!!!!
  • I'll keep that in mind! I'm reading up on passing arguements now. Honestly, I started with PC-DMIS and just started messing with VB so variables are all I really know. I'll have to take some online courses and practice more on the weekend. I'm surprised I got as far as j did to be honest
  • Awesome! You mentioned you're using Visual Studio... watch plenty of "How To" videos on how to use your IDE (integrated development environment). Learn the debugging tools.
  • https://docs.hexagonmi.com/pcdmis/20...broutines.html

    This is the link i used to manage application events. Thought this would be some help
  • I actually signed up for some online courses. (Udemy) And I did some research on passing arguements. I have some ideas on what I can do to use the same code for both buttons while just changing the initial directory in a sub. I may change a few more things also and I'll repost the code so you can see progress. May not have the time until the weekend tho.
    You guys are awesome for helping!
  • This is awesome! I'll keep this in my bookmarks.


  • 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)
        ' 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)
        ' 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)
        ' 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
  • You should be able to use the Enabled property of the buttons.

    For example, in the OnStartExecution section you set the button.Enabled property to False and in the OnEndExecution you set it back to True again. You might want to have some sort of error handling too, if something goes wrong it should enable the buttons again, otherwise they might be disabled until you restart your app (if your app crashes during PC-DMIS program execution or PC-DMIS closes during program execution).
  • That's what I was thinking of doing. I just can't get the onstart and onendexecution to work in testing unless I use it in excel. (Code above your comment.)