none
Referencing same processes? RRS feed

  • Question

  • Hello,

    I have 2 panels, I have a button that opens 2 instances of the same application (same process names) and sets them as child of those panels. Of course, since the timer tick event for each panel (identifies w/ every tick whether or not the process exists) refers to the same process name. How on earth can I differentiate them so that I can set one instance as child to the first panel, and a second identical instance as child to the second panel?

    What I've tried:

    I've tried studying process ID's, however I can't see how this will help me as there doesn't appear to be a way for me to get the procID's beforehand (they're random, right?).

    Please help!
    Thanks in advance.

    Tuesday, November 21, 2017 6:29 PM

Answers

  • Store the handles in a list, or save them to the panels Tag property, for example using notepad and two panels, something like,..

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' get instances of notepad
        Dim p() = Process.GetProcessesByName("notepad")
        If p.Length < 2 Then Return ' must have 2 instances
        ' add notepads to panels
        For n = 1 To 2
            ' get panel on form (Panel1 and Panel2).
            Dim pnl = DirectCast(Me.Controls("Panel" & n.ToString), Panel)
            ' save notepad handle to panel's Tag property.
            pnl.Tag = p(n - 1).MainWindowHandle
            ' set parent
            SetParent(CType(pnl.Tag, IntPtr), pnl.Handle)
        Next
    End Sub
    
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ' Reset parents
        For n = 1 To 2
            Dim pnl = DirectCast(Me.Controls("Panel" & n.ToString), Panel)
            SetParent(CType(pnl.Tag, IntPtr), IntPtr.Zero)
        Next
    End Sub

    • Marked as answer by Lucy Qu Tuesday, November 21, 2017 8:50 PM
    Tuesday, November 21, 2017 8:29 PM

All replies

  • Store the handles in a list, or save them to the panels Tag property, for example using notepad and two panels, something like,..

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' get instances of notepad
        Dim p() = Process.GetProcessesByName("notepad")
        If p.Length < 2 Then Return ' must have 2 instances
        ' add notepads to panels
        For n = 1 To 2
            ' get panel on form (Panel1 and Panel2).
            Dim pnl = DirectCast(Me.Controls("Panel" & n.ToString), Panel)
            ' save notepad handle to panel's Tag property.
            pnl.Tag = p(n - 1).MainWindowHandle
            ' set parent
            SetParent(CType(pnl.Tag, IntPtr), pnl.Handle)
        Next
    End Sub
    
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ' Reset parents
        For n = 1 To 2
            Dim pnl = DirectCast(Me.Controls("Panel" & n.ToString), Panel)
            SetParent(CType(pnl.Tag, IntPtr), IntPtr.Zero)
        Next
    End Sub

    • Marked as answer by Lucy Qu Tuesday, November 21, 2017 8:50 PM
    Tuesday, November 21, 2017 8:29 PM
  • Thanks so much for your reply! :)

    I need a bit more help, I'm using a timer that ticks every 100ms to search for the processes:

        Private Sub SetChildMain(sender As Object, e As EventArgs) Handles SetChildTimer1.Tick
    
            p = Process.GetProcessesByName("ExampleProc").FirstOrDefault
    
            Dim r As New RECT
    
            If p IsNot Nothing Then
    
                GetWindowRect(p.MainWindowHandle, r)
                SetParent(p.MainWindowHandle, Panel1.Handle)
                SendMessage(p.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
    
            End If
    
        End Sub

    The button that I was talking about, when clicked, only 1 process opens, not 2 at once. Also, how can I implement "SendMessage" into your code? I've tried inserting it and modifying it, but I can't seem to modify it to include this without getting an error.

    I've upvoted and marked as answer because people in the future who are looking for something similar will probably find it helpful, however it doesn't quite solve my problem yet. I will be sure to mark it up and as answer if it does.

    Thanks a lot for your time!

    Tuesday, November 21, 2017 8:50 PM
  • Essentially, the only simple solution I can think of is to add an if statement that goes along the lines of:

    If (the process is set as child in the panel) Then
    
    SetChildTimer1.Stop
    ButtonThatStartsAnotherProcess.Enabled = true ' it would be disabled if the process is still loading after it was opened and not quite added to panel yet
    
    End If

    Any way to do this?

    Tuesday, November 21, 2017 9:05 PM