jellyfin/MediaBrowser.Common/Events/EventHelper.cs

109 lines
3.5 KiB
C#
Raw Normal View History

2013-02-21 14:06:23 -07:00
using MediaBrowser.Model.Logging;
2013-02-20 18:33:05 -07:00
using System;
using System.Threading.Tasks;
namespace MediaBrowser.Common.Events
{
/// <summary>
/// Class EventHelper
/// </summary>
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>
2013-02-21 14:06:23 -07:00
/// <param name="logger">The logger.</param>
public static void QueueEventIfNotNull(EventHandler handler, object sender, EventArgs args, ILogger logger)
2013-02-20 18:33:05 -07:00
{
if (handler != null)
{
Task.Run(() =>
{
try
{
handler(sender, args);
}
catch (Exception ex)
{
2013-02-21 14:06:23 -07:00
logger.ErrorException("Error in event handler", ex);
2013-02-20 18:33:05 -07:00
}
});
}
}
/// <summary>
/// Queues the event.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="handler">The handler.</param>
/// <param name="sender">The sender.</param>
/// <param name="args">The args.</param>
2013-02-21 14:06:23 -07:00
/// <param name="logger">The logger.</param>
public static void QueueEventIfNotNull<T>(EventHandler<T> handler, object sender, T args, ILogger logger)
2013-02-20 18:33:05 -07:00
{
if (handler != null)
{
Task.Run(() =>
{
try
{
handler(sender, args);
}
catch (Exception ex)
{
2013-02-21 14:06:23 -07:00
logger.ErrorException("Error in event handler", ex);
2013-02-20 18:33:05 -07:00
}
});
}
}
/// <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>
2013-02-21 14:06:23 -07:00
/// <param name="logger">The logger.</param>
public static void FireEventIfNotNull(EventHandler handler, object sender, EventArgs args, ILogger logger)
2013-02-20 18:33:05 -07:00
{
if (handler != null)
{
try
{
handler(sender, args);
}
catch (Exception ex)
{
2013-02-21 14:06:23 -07:00
logger.ErrorException("Error in event handler", ex);
2013-02-20 18:33:05 -07:00
}
}
}
/// <summary>
/// Fires the event.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="handler">The handler.</param>
/// <param name="sender">The sender.</param>
/// <param name="args">The args.</param>
2013-02-21 14:06:23 -07:00
/// <param name="logger">The logger.</param>
public static void FireEventIfNotNull<T>(EventHandler<T> handler, object sender, T args, ILogger logger)
2013-02-20 18:33:05 -07:00
{
if (handler != null)
{
try
{
handler(sender, args);
}
catch (Exception ex)
{
2013-02-21 14:06:23 -07:00
logger.ErrorException("Error in event handler", ex);
2013-02-20 18:33:05 -07:00
}
}
}
}
}