因为我不确定是什么导致它,我必须复制并粘贴所有代码,对不起,如果这违反了规则。此程序是用Visual Basic 2010编写的,它应该使用cmd来操作Robocopy,然后实时显示结果。它是这样做的,但是在几次使用后挂起,我是这个编码的新手,我不太确定如何处理线程所以它可能是那样但我不知道如何解决它。
如果有人能帮助我,我将非常感激:)
Public Class Form1
Private Results As String
Private Delegate Sub delUpdate()
Private Finished As New delUpdate(AddressOf UpdateText)
Dim button_click As Integer = 0
Dim myProcess As New Process
Dim StartInfo As New System.Diagnostics.ProcessStartInfo
Dim Folder_TBC As String
Dim Folder_DES As String
Dim RoboCommand As String
Private Sub UpdateText()
' TextBox_CMDout.Text = Results
TextBox_CMDout.AppendText(System.Environment.NewLine() & Results)
TextBox_CMDout.ScrollToCaret()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If button_click = 0 Then
RoboCommand = Chr(34) + TextBox_FolderTBC.Text + Chr(34) + " " + Chr(34) + TextBox_FolderDes.Text + Chr(34) + " "
If CheckBox_ISF.Checked = True Then RoboCommand = RoboCommand + "/S "
If CheckBox_MUT.Checked = True Then RoboCommand = RoboCommand + "/MT "
If CheckBox_MOV.Checked = True Then RoboCommand = RoboCommand + "/Move "
opencmd()
button_click += 1
End If
Dim CMDThread As New Threading.Thread(AddressOf CMDAutomate)
CMDThread.Start()
End Sub
Private Sub opencmd()
StartInfo.FileName = "cmd" 'starts cmd window
StartInfo.UseShellExecute = False 'required to redirect
StartInfo.CreateNoWindow = True 'creates no cmd window
StartInfo.RedirectStandardInput = True
StartInfo.RedirectStandardOutput = True
StartInfo.RedirectStandardError = True
myProcess.StartInfo = StartInfo
myProcess.Start()
End Sub
Private Sub CMDAutomate()
'Dim SR As System.IO.StreamReader = myprocess.StandardOutput
'Dim SW As System.IO.StreamWriter = myprocess.StandardInput
myProcess.StandardInput.WriteLine("robocopy " & Folder_TBC & Folder_DES)
myProcess.StandardInput.WriteLine(System.Environment.NewLine())
' SW.WriteLine("exit") 'exits command prompt window
While myProcess.StandardOutput.EndOfStream = False
Results = myProcess.StandardOutput.ReadLine()
Invoke(Finished)
End While
'Results = SR.ReadToEnd 'returns results of the command window
'SW.Close()
'SR.Close()
'invokes Finished delegate, which updates textbox with the results text
Application.ExitThread()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
FolderBrowserDialog1.ShowDialog()
TextBox_FolderTBC.Text = FolderBrowserDialog1.SelectedPath
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
FolderBrowserDialog1.ShowDialog()
TextBox_FolderDes.Text = FolderBrowserDialog1.SelectedPath
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Application.ExitThread()
Application.Exit()
End
End Sub
结束班
答案 0 :(得分:0)
处理下一次点击时清除TextBox_CMDout文本,您将一次又一次地连接所有输出
接下来我认为您的解决方案对于您想要做的事情来说太复杂了。
尝试这个(并从文本框中填写正确的参数),更多是不需要的,并避免使用“何时从CMD命令退出?”这样的主题。和类似的。
Dim myProcess As New Process()
myProcess.StartInfo.FileName = "Robocopy"
myProcess.StartInfo.Arguments = "/?"
myProcess.StartInfo.UseShellExecute = False
myProcess.StartInfo.RedirectStandardOutput = True
myProcess.Start()
TextBox_CMDout.Text = myProcess.StandardOutput.ReadToEnd()
myProcess.WaitForExit()
myProcess.Close()