EventHelper.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using MediaBrowser.Common.Logging;
  2. using System;
  3. using System.Threading.Tasks;
  4. namespace MediaBrowser.Common.Events
  5. {
  6. /// <summary>
  7. /// Class EventHelper
  8. /// </summary>
  9. public static class EventHelper
  10. {
  11. /// <summary>
  12. /// Fires the event.
  13. /// </summary>
  14. /// <param name="handler">The handler.</param>
  15. /// <param name="sender">The sender.</param>
  16. /// <param name="args">The <see cref="EventArgs" /> instance containing the event data.</param>
  17. public static void QueueEventIfNotNull(EventHandler handler, object sender, EventArgs args)
  18. {
  19. if (handler != null)
  20. {
  21. Task.Run(() =>
  22. {
  23. try
  24. {
  25. handler(sender, args);
  26. }
  27. catch (Exception ex)
  28. {
  29. Logger.LogException("Error in event handler", ex);
  30. }
  31. });
  32. }
  33. }
  34. /// <summary>
  35. /// Queues the event.
  36. /// </summary>
  37. /// <typeparam name="T"></typeparam>
  38. /// <param name="handler">The handler.</param>
  39. /// <param name="sender">The sender.</param>
  40. /// <param name="args">The args.</param>
  41. public static void QueueEventIfNotNull<T>(EventHandler<T> handler, object sender, T args)
  42. {
  43. if (handler != null)
  44. {
  45. Task.Run(() =>
  46. {
  47. try
  48. {
  49. handler(sender, args);
  50. }
  51. catch (Exception ex)
  52. {
  53. Logger.LogException("Error in event handler", ex);
  54. }
  55. });
  56. }
  57. }
  58. /// <summary>
  59. /// Fires the event.
  60. /// </summary>
  61. /// <param name="handler">The handler.</param>
  62. /// <param name="sender">The sender.</param>
  63. /// <param name="args">The <see cref="EventArgs" /> instance containing the event data.</param>
  64. public static void FireEventIfNotNull(EventHandler handler, object sender, EventArgs args)
  65. {
  66. if (handler != null)
  67. {
  68. try
  69. {
  70. handler(sender, args);
  71. }
  72. catch (Exception ex)
  73. {
  74. Logger.LogException("Error in event handler", ex);
  75. }
  76. }
  77. }
  78. /// <summary>
  79. /// Fires the event.
  80. /// </summary>
  81. /// <typeparam name="T"></typeparam>
  82. /// <param name="handler">The handler.</param>
  83. /// <param name="sender">The sender.</param>
  84. /// <param name="args">The args.</param>
  85. public static void FireEventIfNotNull<T>(EventHandler<T> handler, object sender, T args)
  86. {
  87. if (handler != null)
  88. {
  89. try
  90. {
  91. handler(sender, args);
  92. }
  93. catch (Exception ex)
  94. {
  95. Logger.LogException("Error in event handler", ex);
  96. }
  97. }
  98. }
  99. }
  100. }