vb.netvisual-studio-2010processwaitforexit

What Would Cause A Form To Freeze Upon Executing Code


I'm trying to figure why my form freezes up when executing some code. I also can't minimize or move the form. Is it because of the WaitForExit being used in the process?

The below code is tied to a button click.

If Checkbox1.checked = True Then
   Call Test()
End If

If Checkbox2.checked = True Then
   Goto NextStep
Else
   Goto StopProcessing
End If

Here is the test sub I'm calling. Calls an exe with an optional argument.

        Using psinfo As New Process
            psinfo.StartInfo.FileName = "C:\Temp\Test.exe "
            psinfo.StartInfo.Arguments = Arg1
            psinfo.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
            psinfo.Start()
            psinfo.WaitForExit()
        End Using

The WaitForExit was added (so I thought) to not process the next statement (next statement being the If statement for Checkbox2) until the process was complete. Is this not the case?


Solution

  • The WaitForExit was added (so I thought) to not process the next statement (next statement being the If statement for Checkbox2) until the process was complete.

    When you call WaitForExit, it will block until the process (Test.exe) completes.

    Since you're running this on the user interface thread, it will cause your form to "freeze" until the process completes fully.

    If you need this to not occur, you would need to wait on a background thread. You could, potentially, move this code into a BackgroundWorker and use it to synchronize with your main window - but you will need to handle "waiting" for the process to finish in a different manner (ie: disable your UI up front, run the process, re-enable when complete).

    Note that, with the Process class, another alternative would be to add EnableRaisingEvents on the process, then adding a handler to Process.Exited. This will let you not WaitForExit(), but instead get notified via an event when the process completes.