C# 在linux中使用Google Protoc编译器时,Process.Start挂起
Process.Start在linux中挂起。我正在从proto文件生成一个C#文件。它在某些文件上运行正常,但偶尔会挂起。此方法正在单独的线程上运行C# 在linux中使用Google Protoc编译器时,Process.Start挂起,c#,asp.net,asp.net-core,protocol-buffers,C#,Asp.net,Asp.net Core,Protocol Buffers,Process.Start在linux中挂起。我正在从proto文件生成一个C#文件。它在某些文件上运行正常,但偶尔会挂起。此方法正在单独的线程上运行 public async Task GenerateCSharpFileAsync(string fileName, string protoFilePath, params string[] args) { var outputFolder = ""; // some path
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():
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;
}
}
公共静态异步任务WaitForExitAsync(此进程,CancellationToken CancellationToken=default)
{
var tcs=new TaskCompletionSource();
无效进程已退出(对象发送方,事件参数)
{
Task.Run(()=>tcs.TrySetResult(true));
}
process.EnableRaisingEvents=true;
process.Exited+=ProcessExited;
尝试
{
if(进程已退出)
{
返回;
}
使用(cancellationToken.Register(()=>Task.Run(()=>tcs.TrySetCanceled(),cancellationToken)))
{
等待tcs任务;
}
}
最后
{
process.Exited-=ProcessExited;
}
}
请协助。由于某种原因,如果该服务是从命令行运行的,例如:dotnet“Test.dll”&作为后台任务,它只是挂起 尝试在没有&的情况下运行dotnet“Test.dll”,效果很好