2012-07-22 26 views
0

如何在vb.net的異步BackgroundWorker進程中運行cmd行實用程序?如何在vb.net的異步BackgroundWorker進程中運行cmd行實用程序?

例子:

Private Sub UpgradeButton_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpgradeButton.Click 
    BackgroundWorker1.RunWorkerAsync() 
End Sub 

Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork 

    Dim MyWorkingDirectory = "C:\Windows\System32\" 
    Dim MyFileName As String = "notepad.exe" 
    Dim MyArguments As String = "" 

    Dim myProcess As System.Diagnostics.Process = New System.Diagnostics.Process() 
    myProcess.StartInfo.WorkingDirectory = MyWorkingDirectory 
    myProcess.StartInfo.FileName = MyFileName 
    myProcess.StartInfo.Arguments = MyArguments 
    myProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden 
    myProcess.Start() 
    ' Wait until it ends. 
    myProcess.WaitForExit() 
    ' Close the process to free resources. 
    myProcess.Close() 

End Sub 
+0

你有什麼問題? – 2012-07-22 12:25:08

回答

0

我不知道爲什麼你的代碼不能正常工作 - 我想,這是行不通的 - 我想別的建議你:使用一個線程,而不是BackgroundWorker的的。如果你只需要啓動應用程序,使用一個簡單的線程:

Public Shared Sub ThreadProc() 
    ''Starth the process here! 
End Sub 


Dim t As New Thread(AddressOf ThreadProc) 
t.IsBackground = True 
t.Start() 

而且啓動線程打開你需要的進程和主線程繼續執行。我希望這是你需要的答案!