流程(robocopy)没有关闭

时间:2013-06-04 11:34:33

标签: c# process

我使用Robocopy将文件从一台计算机移动到另一台计算机。我使用下面的代码:

    public void MoveRecords()
    {
        try
        {
            using (Process Robocopy = new Process())
            {
                Robocopy.StartInfo.FileName = this._commandPromptCommand;
                Robocopy.StartInfo.Arguments = this._commandPromptString;
                Robocopy.StartInfo.UseShellExecute = false;
                Robocopy.StartInfo.CreateNoWindow = true;
                Robocopy.Start();

                DateTime StartTime = DateTime.Now;

                if (Robocopy.WaitForExit(AppSettings.MaxMoveOperationWaitTime))
                {
                    TimeSpan ElapsedTime = DateTime.Now - StartTime;
                    this._logRobocopyExitCode(Robocopy.ExitCode, ElapsedTime);
                }
                else
                {
                    Logger.Write(string.Format("Timeout occured for the move operation of {0} from {1}. ", _getFilesInProgress(), this._ip), EventLogEntryType.Error);
                    Robocopy.Kill();
                }
            }
        }
        catch (Exception ex)
        {
            Logger.Write(ex, EventLogEntryType.Error);
        }
    }

当我看到任务经理时,我看到很多"控制台窗口主机"和#34; Microsoft Robocopy"流程。您可以从下面的屏幕截图中看到这种情况。

enter image description here

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

也许你需要一个“process.close”。以下是我在VB.NET中使用的一个例子:

            ' Create an instance of the command prompt and run RoboCopy for the current network location.
            Try
                pCmdPrompt = New Process
                With pCmdPrompt
                    .StartInfo.FileName = Environment.SystemDirectory & "\RoboCopy.exe"                         ' Resides in System32
                    .StartInfo.Arguments = strDirSrc & " " & strDirDest & strRoboCopyArg & strLogFileFullName
                End With
                pCmdPrompt.Start()              ' Begin RoboCopy
                pCmdPrompt.WaitForExit()        ' Wait for RoboCopy to complete.  Needed in order to obtain DateEnd for Duration details for log.
                pCmdPrompt.Close()              ' RoboCopy completed. Close.
                pCmdPrompt = Nothing            ' Clean up for next loop.

                ' RoboCopy occurred w/o exception. Set string that will be used for log entry.
                strDetailsDone = "Success"

            Catch ex As Exception
                ' Exception occurred during RoboCopy.  Set string that will be used for log entry.
                '   Continue w/ processing - do NOT halt application.
                strDetailsDone = "Exception occurred: " & ex.Message & vbCrLf & ex.StackTrace
                If pCmdPrompt IsNot Nothing Then pCmdPrompt = Nothing
            End Try