一旦命令行中的进程完成,如何打开新表单

时间:2015-11-07 21:37:32

标签: vb.net

我一直试图在一天中最好的时间解决这个问题,我无法解决这个问题。

我之前没有太多代码知识,但在尝试编写任何代码之前,我尝试进行研究。我对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

感谢您收到的所有帮助

1 个答案:

答案 0 :(得分:0)

首先,由于 RoboCopy 是一个命令行界面应用程序(批处理中称为“外部命令”的东西),所以你应该直接调用它,因为不需要调用Windows命令-line只是为了“托管”另一个应用程序。

其次,要解决您遇到的主要问题,可以使用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