【问题标题】:Fail to open file in C# after editing it in another process在另一个进程中编辑文件后无法在 C# 中打开文件
【发布时间】:2015-03-05 11:23:48
【问题描述】:

我正在使用辅助类来运行外部进程:

class ExternalProcessRunner
{
    static public string Run(string program, string parameters)
    {
        output = "";
        error = "";
        try
        {
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.CreateNoWindow = true;
            startInfo.UseShellExecute = false;
            startInfo.FileName = program;
            startInfo.Arguments = parameters;
            startInfo.WindowStyle = ProcessWindowStyle.Hidden;
            startInfo.WindowStyle = ProcessWindowStyle.Minimized;
            startInfo.RedirectStandardError = true;
            startInfo.RedirectStandardOutput = true;

            StringBuilder outputSB = new StringBuilder();
            StringBuilder errorSB = new StringBuilder();

            using (Process exeProcess = Process.Start(startInfo))
            using (AutoResetEvent outputWaitHandle = new AutoResetEvent(false))
            using (AutoResetEvent errorWaitHandle = new AutoResetEvent(false))
            {
                exeProcess.OutputDataReceived += (sender, e) =>
                {
                    if (e.Data == null)
                    {
                        outputWaitHandle.Set();
                    }
                    else
                    {
                        outputSB.AppendLine(e.Data);
                    }
                };
                exeProcess.ErrorDataReceived += (sender, e) =>
                {
                    if (e.Data == null)
                    {
                        errorWaitHandle.Set();
                    }
                    else
                    {
                        errorSB.AppendLine(e.Data);
                    }
                };

                exeProcess.Start();

                exeProcess.BeginOutputReadLine();
                exeProcess.BeginErrorReadLine();

                exeProcess.WaitForExit();
                outputWaitHandle.WaitOne();
                errorWaitHandle.WaitOne();

                output = outputSB.ToString();
                error = errorSB.ToString();
            }
        }
        catch (Exception e)
        {
            return e.Message;
        }

        return "";
    }

    static public string output;
    static public string error;
}

它用于运行 perl 脚本,该脚本接受文件名,打开文件,写入一些信息并关闭文件。然后 C# 代码打开该文件进行读取。有时我会遇到异常: “该进程无法访问文件'tmp_file.txt',因为它正被另一个进程使用。” 什么会导致问题?如何解决?我认为我正在确保进程结束,这意味着释放所有句柄。

【问题讨论】:

  • 可能是脚本没有正确退出(有时),所以文件从那里打开..
  • 了解打开文件的进程将是很好的第一步。在 Windows SysInternal 的 Process Explorer 和 Handles 工具上将为您提供这些信息。

标签: c# perl ipc


【解决方案1】:

请检查您是否已退出进程,即您已从内存中释放文件。

【讨论】:

  • 我执行 WaitForExit,并且 Process 对象由于“使用”而被释放。
【解决方案2】:

您可以通过设定的秒数等待进程退出。然后检查它是否已经退出。如果还没有,请尝试终止该进程。

在下面的示例中,它等待 1 分钟以使进程退出。如果它没有退出,那么它会发出一个关闭主窗口并休眠 2 秒的命令。如果它仍然没有退出,那么它会尝试终止该进程。 在这个阶段,外部进程应该消失并且文件上的锁被释放。

 exeProcess.WaitForExit(1 * 60 * 1000);

if (!exeProcess.HasExited)
{
    _log.Warn("External process has not completed after {0} minutes - trying to close main window", waitTimeMin);
    exeProcess.CloseMainWindow();
    System.Threading.Thread.Sleep(2000);
}

if (!exeProcess.HasExited)
{
    _log.Warn("External process still has not completed - Killing process and waiting for it to exit...");
    exeProcess.Kill();
    exeProcess.WaitForExit();
}

【讨论】:

  • 我不想杀了它。我想等到进程真正退出。
猜你喜欢
  • 1970-01-01
  • 2013-06-10
  • 2020-06-22
  • 2013-06-25
  • 1970-01-01
  • 2021-11-02
  • 1970-01-01
  • 1970-01-01
  • 2014-06-19
相关资源
最近更新 更多