我正在.net 服务中启动一个新进程并在控制台中运行 winrar。它适用于没有大量文件和单个文件的文件夹,但在归档大文件夹时,该过程似乎在某个时候停止。我说它似乎停止了,因为
- 当我取回输出时存档不完整
- 再次启动触发文件夹稀有的主进程
这是运行该过程的一段代码:
Private Sub log(text As String)
IO.File.AppendAllText("log.txt", text)
End Sub
Private Sub SortOutputHandler(sendingProcess As Object, _
outLine As DataReceivedEventArgs)
If Not String.IsNullOrEmpty(outLine.Data) Then
numOutputLines += 1
log(Environment.NewLine & "[" _
& numOutputLines.ToString() & "] - " _
& outLine.Data)
End If
End Sub
Private Function RunCmd(ParamArray commands As String()) As String
Try
If Not sortOutput Is Nothing Then
sortOutput.Length = 0
End If
Dim returnvalue As String = String.Empty
Dim info As New ProcessStartInfo("cmd")
info.UseShellExecute = False
info.RedirectStandardInput = True
info.RedirectStandardOutput = True
info.CreateNoWindow = True
Using process__1 As Process = Process.Start(info)
AddHandler process__1.OutputDataReceived, AddressOf SortOutputHandler
process__1.BeginOutputReadLine()
Using sw As StreamWriter = process__1.StandardInput
For Each command As String In commands
sw.WriteLine("chcp 65001")
sw.WriteLine(command)
log(command)
Next
sw.Close()
End Using
process__1.WaitForExit()
End Using
returnvalue = sortOutput.ToString
info = Nothing
Return returnvalue
Catch ex As Exception
Return Nothing
End Try
End Function
Private Sub zip(destinationFolder As String, outputFile As String, sourceItem As String)
Dim results As String = RunCmd("rar.exe u """ & destinationFolder & outputFile & """ -m3 -w" & workingDir & " """ & sourceItem & """ ")
End Sub
我得到的输出是从命令行运行 rar 时所期望的,但它只是在存档中间的某个时间点结束:
[3945] - Adding \\servername\path1 51% OK
[3946] - Adding \\servername\path2 51%
[3947] - C:\Windows\system32>
存档不完整并且是 System.IO。在服务的主块中引发异常,抱怨它找不到路径的一部分(传递给命令行的源的子文件夹之一)
我想知道输出的异步读取是否正确?在我看来,这是因为我在日志文件中逐行获取输出,但是当我同步读取输出时,我遇到了同样的问题(虽然不能有相同的日志)。
谢谢