【问题标题】:Process.Start hangs in linux while using Google Protoc compiler使用 Google Protoc 编译器时,Process.Start 在 linux 中挂起
【发布时间】:2021-01-13 10:24:27
【问题描述】:

Process.Start 在 linux 中挂起。我正在从 proto 文件生成一个 C# 文件。它在某些文件上运行良好,但偶尔会挂起。此方法在单独的线程上运行。

    public async Task GenerateCSharpFileAsync(string fileName, string protoFilePath, params string[] args)
    {
        var outputFolder = ""; // some path
        var protocPath = GetProtoCompiler();
        var inputs = $" --proto_path={protoFilePath} --csharp_out={outputFolder} --error_format=gcc {fileName} {string.Join(" ", args)}";

        var psi = new ProcessStartInfo(
            protocPath,
            arguments: inputs
        )
        {
            CreateNoWindow = true,
            WindowStyle = ProcessWindowStyle.Hidden,
            WorkingDirectory = Global.WebRoot,
            UseShellExecute = false
        };

        psi.RedirectStandardOutput = psi.RedirectStandardError = true;

        var proc = new Process { StartInfo = psi };

        try
        {
            proc.Start();

            await proc.WaitForExitAsync();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            proc.Close();
            proc.Dispose();
        }
    }

WaitForExitAsync() 从这里被引用:process.WaitForExit() asynchronously

waitforexitasync 的代码:

    public static async Task WaitForExitAsync(this Process process, CancellationToken cancellationToken = default)
    {
        var tcs = new TaskCompletionSource<bool>();

        void ProcessExited(object sender, EventArgs e)
        {
            Task.Run(() => tcs.TrySetResult(true));
        }

        process.EnableRaisingEvents = true;
        process.Exited += ProcessExited;

        try
        {
            if (process.HasExited)
            {
                return;
            }

            using (cancellationToken.Register(() => Task.Run(() => tcs.TrySetCanceled(), cancellationToken)))
            {
                await tcs.Task;
            }
        }
        finally
        {
            process.Exited -= ProcessExited;
        }
    }

请提供帮助。

【问题讨论】:

    标签: c# asp.net asp.net-core protocol-buffers


    【解决方案1】:

    由于某种原因,如果服务从命令行运行,例如:dotnet "Test.dll" & 作为后台任务,它只是挂起。

    尝试在不带 & 的情况下运行 dotnet "Test.dll",它运行良好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-24
      • 2017-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多