| | | 1 | | using System; |
| | | 2 | | using System.Threading.Tasks; |
| | | 3 | | using Microsoft.Extensions.Logging; |
| | | 4 | | |
| | | 5 | | namespace MediaBrowser.Common.Events |
| | | 6 | | { |
| | | 7 | | /// <summary> |
| | | 8 | | /// Class EventHelper. |
| | | 9 | | /// </summary> |
| | | 10 | | // TODO: @bond Remove |
| | | 11 | | public static class EventHelper |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Fires the event. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <param name="handler">The handler.</param> |
| | | 17 | | /// <param name="sender">The sender.</param> |
| | | 18 | | /// <param name="args">The <see cref="EventArgs" /> instance containing the event data.</param> |
| | | 19 | | /// <param name="logger">The logger.</param> |
| | | 20 | | public static void QueueEventIfNotNull(EventHandler? handler, object sender, EventArgs args, ILogger logger) |
| | | 21 | | { |
| | 0 | 22 | | if (handler is not null) |
| | | 23 | | { |
| | 0 | 24 | | Task.Run(() => |
| | 0 | 25 | | { |
| | 0 | 26 | | try |
| | 0 | 27 | | { |
| | 0 | 28 | | handler(sender, args); |
| | 0 | 29 | | } |
| | 0 | 30 | | catch (Exception ex) |
| | 0 | 31 | | { |
| | 0 | 32 | | logger.LogError(ex, "Error in event handler"); |
| | 0 | 33 | | } |
| | 0 | 34 | | }); |
| | | 35 | | } |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Queues the event. |
| | | 40 | | /// </summary> |
| | | 41 | | /// <typeparam name="T">Argument type for the <c>handler</c>.</typeparam> |
| | | 42 | | /// <param name="handler">The handler.</param> |
| | | 43 | | /// <param name="sender">The sender.</param> |
| | | 44 | | /// <param name="args">The args.</param> |
| | | 45 | | /// <param name="logger">The logger.</param> |
| | | 46 | | public static void QueueEventIfNotNull<T>(EventHandler<T>? handler, object sender, T args, ILogger logger) |
| | | 47 | | { |
| | 116 | 48 | | if (handler is not null) |
| | | 49 | | { |
| | 116 | 50 | | Task.Run(() => |
| | 116 | 51 | | { |
| | 116 | 52 | | try |
| | 116 | 53 | | { |
| | 116 | 54 | | handler(sender, args); |
| | 116 | 55 | | } |
| | 116 | 56 | | catch (Exception ex) |
| | 116 | 57 | | { |
| | 116 | 58 | | logger.LogError(ex, "Error in event handler"); |
| | 116 | 59 | | } |
| | 116 | 60 | | }); |
| | | 61 | | } |
| | 116 | 62 | | } |
| | | 63 | | } |
| | | 64 | | } |