Process.Start 在使用 Google Protoc 编译器时挂在 linux
Process.Start hangs in linux while using Google Protoc compiler
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;
}
}
请提供帮助。
出于某种原因,如果服务 运行 来自命令行,如:dotnet "Test.dll" & 作为后台任务,它会挂起。
尝试了 运行ning dotnet "Test.dll" 没有 & 并且工作正常。
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;
}
}
请提供帮助。
出于某种原因,如果服务 运行 来自命令行,如:dotnet "Test.dll" & 作为后台任务,它会挂起。
尝试了 运行ning dotnet "Test.dll" 没有 & 并且工作正常。