DispatherInovke.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using Avalonia.Threading;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace ShakerApp.Tools
  9. {
  10. public static class DispatherInovke
  11. {
  12. public static void Inovke(Action action)
  13. {
  14. if (action == null) return;
  15. try
  16. {
  17. if (Dispatcher.UIThread.CheckAccess())
  18. {
  19. action();
  20. }
  21. else
  22. {
  23. Dispatcher.UIThread.Invoke(action);
  24. }
  25. }
  26. catch
  27. {
  28. }
  29. }
  30. public static T? Inovke<T>(Func<T> action)
  31. {
  32. if (action == null) return default;
  33. try
  34. {
  35. if (Dispatcher.UIThread.CheckAccess())
  36. {
  37. return action();
  38. }
  39. else
  40. {
  41. return Dispatcher.UIThread.Invoke(action);
  42. }
  43. }
  44. catch
  45. {
  46. }
  47. return default;
  48. }
  49. //public static void Inovke(this Action action)
  50. //{
  51. // if (action == null) return;
  52. // if (App.Current.Dispatcher.Thread == Thread.CurrentThread)
  53. // {
  54. // action();
  55. // }
  56. // else
  57. // {
  58. // App.Current?.Dispatcher?.Invoke(action);
  59. // }
  60. //}
  61. }
  62. }