Process.WaitForExit dynamic

2

I wonder if there is any way to know if the process came out, even while continuing project execution.

When you use Processo.WaitForExit it to the application, its execution is stopped.

I would like to know if there is a way to use this WaitForExit , keeping the execution, eg:

Dim i As Process = New Process()
i.StartInfo.FileName = "Meu executável"
i.UseShellExecute = False
i.Start
i.WaitForExit

'Agora queria que o aplicativo funcionasse normalmente, sem que os botões fiquem congelados...
MsgBox("Aplicativo saiu.", 0)
    
asked by anonymous 25.05.2015 / 21:31

1 answer

1

This happens because when you use the method Process.WaitForExit , you run it in the main thread >, which is responsible for redesigning the window, receiving messages, etc. Your application is still running, but it does not refresh the window, which makes it look frozen.

To work around this problem, you can create the process in another thread , or in a thread pool, or run the href="http://en.wikipedia.org/wiki/Compare_parallel"> parallel mode using Task library . Here is an example of the latter:

Public Sub executarArquivo(arquivo As String, argumentos As String)
    Dim processo As Process = New Process()
    processo.StartInfo.FileName = arquivo
    processo.StartInfo.Arguments = argumentos
    processo.StartInfo.UseShellExecute = False
    processo.Start()

    processo.WaitForExit()
End Sub

To call the function, do the following:

Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Await Task.Run(Sub()
                       executarArquivo("Meu executável", "")
                   End Sub)

    MsgBox("Aplicativo saiu.", 0)
End Sub
    
26.05.2015 / 06:34