Refactor `NfcHanler` namespace to `NfcHandler` and enhance code consistency.
36 lines
1.1 KiB
C#
36 lines
1.1 KiB
C#
using System.Diagnostics;
|
|
|
|
namespace NfcC7_DLL.NfcHandler.Utils
|
|
{
|
|
public static class ProcessExtensions
|
|
{
|
|
public static Task WaitForExitAsync(this Process process, CancellationToken cancellationToken = default)
|
|
{
|
|
if (process == null) throw new ArgumentNullException(nameof(process));
|
|
if (process.HasExited) return Task.CompletedTask;
|
|
|
|
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;
|
|
}
|
|
}
|
|
} |