2013-06-18 63 views
1

我正在启动.NET服务中的新进程并在控制台中运行winrar。 它适用于没有大量文件和单个文件的文件夹,但是当归档大文件夹时,该过程似乎在某个时刻停止。 我说的似乎停止,因为过程意外结束

  1. 当我得到的输出回
  2. 主要过程触发的文件夹铆足了劲的档案是不完整的重新开始

这里是一片运行过程的代码:

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

我想知道是否异步读取的输出是正确? 在我看来,这是因为我在日志文件中逐行获取输出,但是当我同步读取输出时,我遇到了同样的问题(尽管不能有相同的日志)。

感谢

回答

0

的问题是在这部分代码:

Catch ex As Exception 
    Return Nothing 
End Try 

可能发生的任何异常,只是忽略,并且函数立即返回。你至少可以返回异常信息,而不是Nothing,这可能会给你提示发生了什么,以及如何解决这种情况。

+0

这是一个稍微削减的代码版本,在该catch中有一个额外的调用来记录异常。从本质上来说,在这部分代码中没有例外,我得到的唯一例外是在我的服务的主要块中调用zip Sub。由于服务的主要部分被重复的唯一原因是因为rar首先没有完成,我认为它可能与它无关 – iasonas