tbf/NfcC7_DLL/NfcHandler/Utils/ProcessExtensions.cs
Michal Buzik f1e6e94450 work backup
- Register Reader Poseidon works
- Smart Form - wrong functionality
- refactoring
2025-11-06 14:11:13 +01:00

39 lines
1.2 KiB
C#

using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace NfcC7_DLL.NfcHandler.Utils
{
public static class ProcessExtensions
{
public static Task<object> WaitForExitAsync(this Process process, CancellationToken cancellationToken = default)
{
if (process == null) throw new ArgumentNullException(nameof(process));
if (process.HasExited) return Task.FromResult<object>(null);
var tcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
EventHandler handler = null;
handler = (s, e) =>
{
try { tcs.TrySetResult(null); }
finally { process.Exited -= handler; }
};
process.EnableRaisingEvents = true;
process.Exited += handler;
if (cancellationToken.CanBeCanceled)
{
cancellationToken.Register(() =>
{
tcs.TrySetCanceled(cancellationToken);
process.Exited -= handler;
});
}
return tcs.Task;
}
}
}