流程意外结束

时间:2013-06-18 09:11:33

标签: .net

我在.net服务中启动一个新进程并在控制台中运行winrar。 它适用于没有大量文件和单个文件的文件夹,但在归档大文件夹时,进程似乎在某个时刻停止。 我说它似乎停止了因为

  1. 我收到输出后存档未完成
  2. 触发文件夹raring的主进程再次启动
  3. 以下是运行该过程的代码段:

    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。在服务的主块中引发异常,抱怨它无法找到路径的一部分(源传递给命令行的子文件夹之一)

    我想知道输出的异步读取是否正确? 在我看来,就像我在日志文件中逐行获取输出一样,但是当我同步读取输出时,我遇到了同样的问题(虽然没有相同的日志)。

    由于

1 个答案:

答案 0 :(得分:0)

问题在于代码的这一部分:

Catch ex As Exception
    Return Nothing
End Try

可能发生的任何异常都会被忽略,并且函数会立即返回。你至少可以返回异常消息而不是Nothing,这有希望给你一个关于发生了什么的提示,以及如何解决这种情况。