PlatformThreadingInterface.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using System.Threading;
  3. using Avalonia.Platform;
  4. using Avalonia.Threading;
  5. using CoreFoundation;
  6. using Foundation;
  7. namespace Avalonia.iOS
  8. {
  9. class PlatformThreadingInterface : IPlatformThreadingInterface
  10. {
  11. private bool _signaled;
  12. public static PlatformThreadingInterface Instance { get; } = new PlatformThreadingInterface();
  13. public bool CurrentThreadIsLoopThread => NSThread.Current.IsMainThread;
  14. public event Action<DispatcherPriority?> Signaled;
  15. public void RunLoop(CancellationToken cancellationToken)
  16. {
  17. //Mobile platforms are using external main loop
  18. throw new NotSupportedException();
  19. }
  20. public IDisposable StartTimer(DispatcherPriority priority, TimeSpan interval, Action tick)
  21. => NSTimer.CreateRepeatingScheduledTimer(interval, _ => tick());
  22. public void Signal(DispatcherPriority prio)
  23. {
  24. lock (this)
  25. {
  26. if(_signaled)
  27. return;
  28. _signaled = true;
  29. }
  30. DispatchQueue.MainQueue.DispatchAsync(() =>
  31. {
  32. lock (this)
  33. _signaled = false;
  34. Signaled?.Invoke(null);
  35. });
  36. }
  37. }
  38. }