How to call an External program to run ?

itayzoro

Member
Joined
Jul 28, 2008
Messages
23
Programming Experience
Beginner
Hi
How can i call an external program to run and i dont have to wait for the Return Value
The vb code run and when the External prog. will finish its result will influence the vb code (But no wait Somthing like multithreading)

Here an Example :

VB.NET:
Sub msg1() 
        Dim x As String 
        x = "Im First Thread" 
        MsgBox(x) 
    End Sub 

    
Sub msg2() 
        Dim y As String 
        y = "Im Second Thread" 
        MsgBox(y) 
    End Sub 
     
Sub msg3() 
        Dim z As String 
        z = "Im Third Thread" 
        MsgBox(z) 
    End Sub 
     
Sub msg4() 
        Dim s As String 
        s = "Im Fourth Thread" 
        MsgBox(s) 
    End Sub 

        Dim thread1 As Thread 
        Dim thread2 As Thread 
        Dim thread3 As Thread 
        Dim thread4 As Thread 

        thread1 = New Thread(AddressOf msg1) 
        thread2 = New Thread(AddressOf msg2) 
        thread3 = New Thread(AddressOf msg3) 
        thread4 = New Thread(AddressOf msg4) 


        thread1.Start() 
        thread2.Start() 
        thread3.Start() 
        thread4.Start() 
Shell("c:\windows\notepad.exe")
 
Last edited:
You can use the Process class to start it, and either use WaitForExit method in a thread or its Exited event.
 
Another way to do it is:
Dim Prc As New Process = Process.Start("c:\windows\notepad.exe")
Prc.WaitForExit
 
You can find many examples when you search the given keywords, both in help documentation, at these forums, and other internet searches. Here is one: Visual Basic .NET Forums - View Single Post - Shell, wait & terminate

Hello,

I have found this snippet very useful as it nearly covers exactly what I am trying to do. However I am unable to get the code to wait for the process to finish.

If I do this code here

VB.NET:
Dim p As New Process
p.StartInfo.FileName = "c:\test.bat"
p.StartInfo.CreateNoWindow = False
p.EnableRaisingEvents = True
AddHandler p.Exited, AddressOf process_exited
p.Start()

All my code after this continues to run even though the process is still running.

I have tried to use p.start.watforexit but waitforexit is not available?
 
That's because your code will continue to run and when the other process has exited, your 'process_exited' sub will run then.
VB.NET:
Dim p As New Process
p.StartInfo.FileName = "c:\test.bat"
p.StartInfo.CreateNoWindow = False
p.Start()
p.WaitForExit
 
Back
Top