mirror of
https://github.com/Relintai/mono.git
synced 2024-11-08 10:12:16 +01:00
39 lines
976 B
C#
39 lines
976 B
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace GodotTools.Core
|
|
{
|
|
public static class ProcessExtensions
|
|
{
|
|
public static async Task WaitForExitAsync(this Process process, CancellationToken cancellationToken = default(CancellationToken))
|
|
{
|
|
var tcs = new TaskCompletionSource<bool>();
|
|
|
|
void ProcessExited(object sender, EventArgs e)
|
|
{
|
|
tcs.TrySetResult(true);
|
|
}
|
|
|
|
process.EnableRaisingEvents = true;
|
|
process.Exited += ProcessExited;
|
|
|
|
try
|
|
{
|
|
if (process.HasExited)
|
|
return;
|
|
|
|
using (cancellationToken.Register(() => tcs.TrySetCanceled()))
|
|
{
|
|
await tcs.Task;
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
process.Exited -= ProcessExited;
|
|
}
|
|
}
|
|
}
|
|
}
|