AsyncExecution.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.HLE.HOS.Kernel.Threading;
  3. using System;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. namespace Ryujinx.HLE.HOS.Services.Account.Acc.AsyncContext
  7. {
  8. class AsyncExecution
  9. {
  10. private readonly CancellationTokenSource _tokenSource;
  11. private readonly CancellationToken _token;
  12. public KEvent SystemEvent { get; }
  13. public bool IsInitialized { get; private set; }
  14. public bool IsRunning { get; private set; }
  15. public AsyncExecution(KEvent asyncEvent)
  16. {
  17. SystemEvent = asyncEvent;
  18. _tokenSource = new CancellationTokenSource();
  19. _token = _tokenSource.Token;
  20. }
  21. public void Initialize(int timeout, Func<CancellationToken, Task> taskAsync)
  22. {
  23. Task.Run(async () =>
  24. {
  25. IsRunning = true;
  26. _tokenSource.CancelAfter(timeout);
  27. try
  28. {
  29. await taskAsync(_token);
  30. }
  31. catch (Exception ex)
  32. {
  33. Logger.Warning?.Print(LogClass.ServiceAcc, $"Exception: {ex.Message}");
  34. }
  35. SystemEvent.ReadableEvent.Signal();
  36. IsRunning = false;
  37. }, _token);
  38. IsInitialized = true;
  39. }
  40. public void Cancel()
  41. {
  42. _tokenSource.Cancel();
  43. }
  44. }
  45. }