Process.Start在使用Google Protoc编译器时挂起在linux中



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((:进程。WaitForExit((异步

waitforesitasync的代码:

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;
}
}

请协助。

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

尝试运行dotnet";测试.dll";没有&而且效果很好。

最新更新