SetParent acting funny

Piller187

Member
Joined
Feb 8, 2006
Messages
6
Programming Experience
3-5
I have this small chunk of code in an MDI form. When I press a menu item it does this:
VB.NET:
        Me.SuspendLayout()
        Dim runProcess As New System.Diagnostics.Process
        Dim info As New System.Diagnostics.ProcessStartInfo

        info.FileName = "NotePad.exe"
        info.WindowStyle = ProcessWindowStyle.Normal

        runProcess = Process.Start(info)

        SetParent(runProcess.MainWindowHandle, Me.Handle)

        Me.ResumeLayout()

The idea is to have a totally seperate program run inside an MDI for. If I just run this code it doesn't work. However, if I put a break point on the SetParent, and when it goes there F5 to continue it then works. It's almost like a timing thing. Like SetParent() can't set it right away. Does anyone have any advise? Putting any sort of sleep or wait really wouldn't be a great solution. This just seems very strange to me. Thanks in advance.
 
I did this little check and it appears Start doesn't wait until the process has a handle, cause the messagebox comes up.

VB.NET:
 Me.SuspendLayout()
        Dim runProcess As New System.Diagnostics.Process
        Dim info As New System.Diagnostics.ProcessStartInfo

        info.FileName = "NotePad.exe"
        info.WindowStyle = ProcessWindowStyle.Normal

        runProcess = Process.Start(info)

        If runProcess.MainWindowHandle <> 0 Then
            SetParent(runProcess.MainWindowHandle, Me.Handle)
        Else
            MsgBox("No window handle")
        End If

        Me.ResumeLayout()
 
What if you waited 1 second with 'WaitForInputIdle(1000)' then 'SetParent' ? Worth a try perhaps, maybe when step-through there is a little more time for the other process to load it's main window..
 
Well, use the overload that waits indefinitely, but only until other process is loaded. Does also jump out if it is not a 'valid' process. This is not hacky.
VB.NET:
If runProcess.WaitForInputIdle = True Then
  SetParent(runProcess.MainWindowHandle, Me.Handle)
Else
  'immediately returned False, process didn't have a message loop
  '(because it didn't have an user interface)
  MsgBox("No window handle")
End If
 
That actually doesn't work for non user interface programs. Try it with cmd in .NET 2005. I know every process that is getting started so I won't run into this. Thanks.
 
Back
Top