2013-02-20 18:33:05 -07:00
|
|
|
|
using MediaBrowser.Common.Logging;
|
2013-02-21 13:26:35 -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
|
|
|
|
|
{
|
2013-02-21 13:26:35 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The logger
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static readonly ILogger Logger = LogManager.GetLogger("EventHelper");
|
|
|
|
|
|
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>
|
|
|
|
|
public static void QueueEventIfNotNull(EventHandler handler, object sender, EventArgs args)
|
|
|
|
|
{
|
|
|
|
|
if (handler != null)
|
|
|
|
|
{
|
|
|
|
|
Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
handler(sender, args);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2013-02-21 13:26:35 -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>
|
|
|
|
|
public static void QueueEventIfNotNull<T>(EventHandler<T> handler, object sender, T args)
|
|
|
|
|
{
|
|
|
|
|
if (handler != null)
|
|
|
|
|
{
|
|
|
|
|
Task.Run(() =>
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
handler(sender, args);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2013-02-21 13:26:35 -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>
|
|
|
|
|
public static void FireEventIfNotNull(EventHandler handler, object sender, EventArgs args)
|
|
|
|
|
{
|
|
|
|
|
if (handler != null)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
handler(sender, args);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2013-02-21 13:26:35 -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>
|
|
|
|
|
public static void FireEventIfNotNull<T>(EventHandler<T> handler, object sender, T args)
|
|
|
|
|
{
|
|
|
|
|
if (handler != null)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
handler(sender, args);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2013-02-21 13:26:35 -07:00
|
|
|
|
Logger.ErrorException("Error in event handler", ex);
|
2013-02-20 18:33:05 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|