Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have been working on a game in my free time for a while now. The game is a continuum of Histacom. Below is the code I am using to start a program in the game. The process is started with the exepath and arguments passed. The thread pauses for a half second to allow the new process to start. Then SetParent() grabs the process's MainWindowHandle and puts the process inside of a panel on the form. I then have the thread pause again for 5 seconds (this will be reduced). Finally the form is resized to fit the new process's main window. The code works fine as long as I have that Message-box in, but if I comment it out then the process won't go inside the panel or execute the rest of the code. The code works fine on my desktop, but not my tablet, which is slower. So is it that I need to pause more and configure something based on the computers specs or something else? If you need any clarification, just comment.
Thanks for your time

What I have tried:

   Public Sub New(ByRef exePath As String, ByRef argurment As String)
        InitializeComponent()
Dim info As ProcessStartInfo = New ProcessStartInfo(exePath, argurment)
            info.UseShellExecute = False
            info.CreateNoWindow = True
            info.RedirectStandardInput = False
            info.RedirectStandardOutput = False
            info.RedirectStandardError = False
            'info.WindowStyle = ProcessWindowStyle.Hidden
            pro = System.Diagnostics.Process.Start(info)
        Try
run:        System.Threading.Thread.Sleep(500)
            SetParent(pro.MainWindowHandle, Me.panGuestMod.Handle) '<DllImport("user32.dll")>      Public Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr     End Function
             Nap(5000, False) 'Pauses program for 5 sec
            'MessageBox.Show("Program only works if this message box is uncommented")
            GetClientRect(pro.MainWindowHandle, siz) '<DllImport("user32.dll")>     Private Shared Function GetClientRect(ByVal HWND As IntPtr, ByRef LPRECT As Rectangle) As Boolean    End Function
             me.Size = New Size(siz.Width, siz.Height)
 Catch ex As Exception
            MessageBox.Show(ex.ToString)
            GoTo run
        End Try
    End Sub
Posted
Updated 28-Aug-19 20:05pm

1 solution

I'm not going anywhere near that code.
Try without Catch , GoTo instead of loops, two different ways to annoy the user by stopping your app running: Sleep and Nap, assumptions that "something will be finished if I just wait around long enough", ...

That doesn't seem to have been thought about, and is very poor quality - the only way out of the method is when something fails, and you have no idea what failed when it did!

Start by getting rid of the GoTo, and add a Catch block - use the debugger, and find out where it is failing, and exactly why - the Catch block will help you because it includes info as to why it failed and you need that to work out what is going on.
 
Share this answer
 
Comments
Maciej Los 29-Aug-19 2:12am    
Holy true!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900