2015-11-07 58 views
0

我一直在試圖弄清楚現在每天的最佳狀態,而且我無法圍繞這個問題進行解決。如何在命令行中完成一次處理後打開新窗體

我沒有太多先前的代碼知識,但在嘗試編寫任何代碼之前,我試圖進行研究。我有VB.net是如何工作的,但需要一些幫助,我的代碼

Public Class Form1 
Dim CopyFrom As String 
Dim CopyTo As String 
Dim RoboCopyVariables As String 
Dim CopyS As String 
Dim CopyE As String 
Dim WaittimeTXT As String 
Dim WaitTime As String 
Dim RetryAttemptsTXT As String 
Dim RetryAttempts As String 
Dim pProcess() As Process = System.Diagnostics.Process.GetProcessesByName("CMD") 

Private Sub RunBTN_Click(sender As Object, e As EventArgs) Handles RunBTN.Click 
    CopyFrom = CopyFromTXT.Text 
    CopyTo = CopyToTXT.Text 
    'All of the selections for if CopyS has been checked 
    If CopySFoldersCB.Checked = True Then 
     CopyS = " /s" 
    ElseIf CopySFoldersCB.Checked = False Then 
     CopyS = "" 
    End If 
    If CopyEFoldersCB.Checked = True Then 
     CopyE = " /e" 
    ElseIf CopyEFoldersCB.Checked = False Then 
     CopyE = "" 
    End If 
    If WaitTimeCB.Checked = True Then 
     WaittimeTXT = WaitTimeTXT1.Text 
     WaitTime = " /w:" & WaittimeTXT 
    ElseIf WaitTimeCB.Checked = False Then 
     WaitTime = "" 
    End If 
    If RetryAttemptsCB.Checked = True Then 
     RetryAttemptsTXT = RetryAttempts1.Text 
     RetryAttempts = " /r:" & RetryAttemptsTXT 
    ElseIf RetryAttemptsCB.Checked = False Then 
     RetryAttempts = "" 
    End If 
    RoboCopyVariables = CopyS + CopyE + WaitTime + RetryAttempts 
    'CommandTest.Text = "/k robocopy " + CopyFrom + " " + CopyTo + "" + RoboCopyVariables 

    Process.Start("CMD", "/k robocopy " + CopyFrom + " " + CopyTo + "" + RoboCopyVariables) 

    For Each p As Process In pProcess 
     p.Kill() 
    Next 

    Form2.Show() 
    Me.Hide() 


End Sub 

Private Sub CancelButton_Click(sender As Object, e As EventArgs) Handles CancelButton.Click 
    Me.Close() 
    Form2.Close 
End Sub 
End Class 

謝謝所有幫助我收到

+0

當您使用/ k選項時,無法找到它是完整的。改用/ c。或者直接運行它而不使用cmd.exe。使用Process.Exited事件來檢測完成,使用Process.SynchronizingObject屬性讓它在UI線程上運行。 *不*殺死所有cmd.exe進程,你不知道你是否啓動它們。 –

回答

0

首先一個基本的瞭解,因爲ROBOCOPY是一個命令行界面應用程序(在Batch中被稱爲「外部命令」的東西),那麼您應該直接調用它,因爲不需要僅僅調用Windows命令行來「託管」另一個應用程序。

其次,要解決您遇到的主要問題,您可以使用Process.WaitForExit方法,這將阻止當前線程,直到進程退出。

Dim p As New Process 
p.StartInfo.FileName = "Robocopy.exe" 
p.StartInfo.Arguments = String.Format("""{0}"" ""{1}"" {2}", 
             CopyFrom, CopyTo, RoboCopyVariables) 

p.Start() 
p.WaitForExit(-1) 

另一個解決方案是使過程引發事件,然後訂閱Process.Exited事件,這將導致非線程阻塞機制好異步場景。

Public Class yourClassName 

    Friend WithEvents P As New Process 

    ' Call "Test" method somewhere... 
    Sub Test() 

     Dim copyFrom As String = ... 
     Dim copyTo As String = ... 
     Dim roboCopyVariables As String = ... 

     Me.P.EnableRaisingEvents = True 
     Me.P.StartInfo.FileName = "Robocopy.exe" 
     Me.P.StartInfo.Arguments = String.Format("""{0}"" ""{1}"" {2}", 
               copyFrom, copyTo, roboCopyVariables) 

     Me.P.Start() 
     ' Can do any thing here while asynchronouslly we wait for the process to exit... 

    End Sub 

    Private Sub P_Exited(ByVal sender As Object, ByVal e As EventArgs) _ 
    Handles P.Exited 

     ' Show the form here... 
     Form2.Show() 
     Me.Hide() 

    End Sub 

End Class 
+0

感謝您的回覆! 當使用您提供的代碼時,我決定採用第二種解決方案。 雖然我目前收到一些錯誤。 朋友With事件P作爲新工藝 這條線特別。 我得到'Friend'和'WithEvents'的錯誤 我得到的錯誤是''在局部變量聲明中是無效的 – DaMrZ

+0

@DaMrZ'Friend'和/或'WithEvents'只能在方法塊外聲明,只是在方法之外聲明對象。如果您仍然聲明在方法塊中聲明對象,則使用'Dim'(單獨),然後使用'AddHandler'語句將處理程序與'p.Exited'事件相關聯:https://msdn.microsoft.com/ en-us/library/7taxzxka.aspx。如果解決了問題,請考慮將答案標記爲已接受。 – ElektroStudios

+0

我在方法之外聲明瞭它,但是現在我收到錯誤'聲明期望值'。對於每個P 另外CopyFrom也給出了該錯誤以及 – DaMrZ

相關問題