2019-01-13 20:01:16 +00:00
|
|
|
using System;
|
2018-12-27 23:27:57 +00:00
|
|
|
using System.Threading.Tasks;
|
2019-01-13 19:25:11 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2018-12-27 23:27:57 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.Common.Events
|
|
|
|
{
|
|
|
|
/// <summary>
|
2019-12-10 23:13:57 +00:00
|
|
|
/// Class EventHelper.
|
2018-12-27 23:27:57 +00:00
|
|
|
/// </summary>
|
2019-12-10 23:13:57 +00:00
|
|
|
// TODO: @bond Remove
|
2018-12-27 23:27:57 +00:00
|
|
|
public static class EventHelper
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Fires the event.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="handler">The handler.</param>
|
|
|
|
/// <param name="sender">The sender.</param>
|
|
|
|
/// <param name="args">The <see cref="EventArgs" /> instance containing the event data.</param>
|
|
|
|
/// <param name="logger">The logger.</param>
|
2021-05-05 10:57:01 +00:00
|
|
|
public static void QueueEventIfNotNull(EventHandler? handler, object sender, EventArgs args, ILogger logger)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
if (handler != null)
|
|
|
|
{
|
|
|
|
Task.Run(() =>
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
handler(sender, args);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-12-20 12:11:26 +00:00
|
|
|
logger.LogError(ex, "Error in event handler");
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Queues the event.
|
|
|
|
/// </summary>
|
2019-12-10 23:13:57 +00:00
|
|
|
/// <typeparam name="T">Argument type for the <c>handler</c>.</typeparam>
|
2018-12-27 23:27:57 +00:00
|
|
|
/// <param name="handler">The handler.</param>
|
|
|
|
/// <param name="sender">The sender.</param>
|
|
|
|
/// <param name="args">The args.</param>
|
|
|
|
/// <param name="logger">The logger.</param>
|
2021-05-05 10:57:01 +00:00
|
|
|
public static void QueueEventIfNotNull<T>(EventHandler<T>? handler, object sender, T args, ILogger logger)
|
2018-12-27 23:27:57 +00:00
|
|
|
{
|
|
|
|
if (handler != null)
|
|
|
|
{
|
|
|
|
Task.Run(() =>
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
handler(sender, args);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2018-12-20 12:11:26 +00:00
|
|
|
logger.LogError(ex, "Error in event handler");
|
2018-12-27 23:27:57 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|