Dynamic Waitforexit process.

Asked

Viewed 45 times

2

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

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

I wonder if there’s a way to use this WaitForExit, keeping the execution, example:

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)

1 answer

1


This is because when using method Process.WaitForExit, you perform it on thread leading, who is responsible for redesign window, receive messages, etc. Your application is still running, but does not update the window, which makes it appear to be frozen.

To get around this problem, you can create the process in another thread, or in a pool of threads, or perform the parallel mode using the task library. See 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 so:

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
  • 1

    It worked, thanks....

Browser other questions tagged

You are not signed in. Login or sign up in order to post.