Files
DefendNJ/Assets/Plugins/ThirdParty/AsyncAwaitUtil/Source/AwaitExtensions.cs
2025-09-04 14:31:28 +08:00

36 lines
949 B
C#

using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Debug = UnityEngine.Debug;
public static class AwaitExtensions
{
public static TaskAwaiter<int> GetAwaiter(this Process process)
{
var tcs = new TaskCompletionSource<int>();
process.EnableRaisingEvents = true;
process.Exited += (s, e) => tcs.TrySetResult(process.ExitCode);
if (process.HasExited)
{
tcs.TrySetResult(process.ExitCode);
}
return tcs.Task.GetAwaiter();
}
// Any time you call an async method from sync code, you can either use this wrapper
// method or you can define your own `async void` method that performs the await
// on the given Task
public static async void WrapErrors(this Task task)
{
try {
await task;
} catch (Exception e) {
Debug.LogError(e);
}
}
}