Shell, wait & terminate

Xancholy

Well-known member
Joined
Aug 29, 2006
Messages
143
Programming Experience
Beginner
I found this code to shell, wait and terminate an exe

I need to run a command line like:
VB.NET:
mplayer -vfm ffmpeg movie1.avi movie2.avi -vfm dmo
where mplayer is the exe file.

What is the best solution to shell to mplayer and wait till process ends with vb.net ?


Thanks
 
Last edited:
Use the Exited event of Process class. If you run multiple processes and want to see which is which when they finish you can use a Dictionary where you associate the process with the info (for example "movie2").
 
Is this the code or am I missing something ?

VB.NET:
Imports System
Imports System.Diagnostics
Imports System.ComponentModel
Imports System.Threading
Imports Microsoft.VisualBasic
Imports System.IO
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim myProcess As Process = New Process()
        Dim s As String
        myProcess.StartInfo.FileName = "cmd.exe"

        myProcess.StartInfo.UseShellExecute = False
        myProcess.StartInfo.CreateNoWindow = True
        myProcess.StartInfo.RedirectStandardInput = True
        myProcess.StartInfo.RedirectStandardOutput = True
        myProcess.StartInfo.RedirectStandardError = True
        myProcess.Start()

        Dim sIn As StreamWriter = myProcess.StandardInput
        Dim sOut As StreamReader = myProcess.StandardOutput
        Dim sErr As StreamReader = myProcess.StandardError

        sIn.AutoFlush = True
        sIn.Write("mplayer -vfm ffmpeg movie1.avi movie2.avi -vfm dmo" & System.Environment.NewLine)
        sIn.Write("exit" & System.Environment.NewLine)
        s = sOut.ReadToEnd()

        If Not myProcess.HasExited Then
            myProcess.Kill()
        End If

        MessageBox.Show("The 'dir' command window was closed at: " & myProcess.ExitTime & "." & System.Environment.NewLine & "Exit Code: " & myProcess.ExitCode)

        sIn.Close()
        sOut.Close()
        sErr.Close()
        myProcess.Close()
        MessageBox.Show(s)
    End Sub
End Class
 
No, that looks wrong to me. You should start the mplayer process given arguments and use the Exited event to get notified when it has finished.
 
There's also
VB.NET:
System.Diagnostics.Process.Start("mplayer.exe", "-vfm ffmpeg movie1.avi movie2.avi -vfm dmo").WaitForExit()
but this doesn't look right
 
No, that will block the UI thread. It isn't necessary when you have an event to relate to.
 
Using events is really easy and convenient in VB.Net, both using WithEvents variables and dynamically using the AddHandler statement.
VB.NET:
Dim p As New Process
p.StartInfo.FileName = "app.exe"
p.StartInfo.Arguments = "the arguments"
p.EnableRaisingEvents = True
AddHandler p.Exited, AddressOf process_exited
p.Start()
VB.NET:
Sub process_exited(ByVal sender As Object, ByVal e As EventArgs)
    MsgBox("some process finished")
End Sub
As in all events the sender parameter of the event handler is the class instance that raised the event. This enables you to use one handler method to listen to multiple objects and can always identify which instance currently is notifying.
 
Thanks. Elegant.

Assume I supply app.exe by adding it to my project as a resource...

How do I call it ?
VB.NET:
p.StartInfo.FileName = [COLOR="Red"]??? +[/COLOR] "app.exe"

Also how do I prevent the cmd window from making an appearance ?
 
Last edited:
Filename should be the full path to the executable.
cmd window ?
 
mplayer.exe allows command line interface

Assume I supply mplayer.exe by adding it to my project as a resource...

Should it be called like this?
VB.NET:
p.StartInfo.FileName = Application.startuppath + "mplayer.exe"

running this process brings up the cmd.exe window where we see the dos progress. Can it be supressed using
VB.NET:
p.StartInfo.CreateNoWindow = True

but the window comes up anyway
 
So mplayer is a console application... setting UseShellExecute=True and WindowStyle=Hidden did it for me with a console app. Don't be afraid to read documentation and experiment with code!

You can't run executables from resources, you have to run it from file system.
 
you have to run it from file system.

So that means I have to provide an absolute path to the console app even though it will be deployed and physically available in application.startuppath ?

I am experimenting more than ever:eek:
 
Application.StartupPath is a dynamic property that returns an absolute path, not a relative path.
 
Back
Top