jellyfin-server/MediaBrowser.Common/Progress/ActionableProgress.cs

75 lines
2.0 KiB
C#
Raw Normal View History

2013-02-21 01:33:05 +00:00
using System;
using System.Collections.Generic;
namespace MediaBrowser.Common.Progress
{
/// <summary>
/// Class ActionableProgress
/// </summary>
/// <typeparam name="T"></typeparam>
2017-06-23 16:04:45 +00:00
public class ActionableProgress<T> : IProgress<T>, IDisposable
2013-02-21 01:33:05 +00:00
{
/// <summary>
/// The _actions
/// </summary>
private readonly List<Action<T>> _actions = new List<Action<T>>();
2017-06-23 16:04:45 +00:00
public event EventHandler<T> ProgressChanged;
2013-02-21 01:33:05 +00:00
/// <summary>
/// Registers the action.
/// </summary>
/// <param name="action">The action.</param>
public void RegisterAction(Action<T> action)
{
_actions.Add(action);
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
Dispose(true);
2017-09-05 19:49:02 +00:00
GC.SuppressFinalize(this);
2013-02-21 01:33:05 +00:00
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_actions.Clear();
}
}
2017-06-23 16:04:45 +00:00
public void Report(T value)
{
if (ProgressChanged != null)
{
ProgressChanged(this, value);
}
foreach (var action in _actions)
{
action(value);
}
}
}
public class SimpleProgress<T> : IProgress<T>
{
public event EventHandler<T> ProgressChanged;
public void Report(T value)
{
if (ProgressChanged != null)
{
ProgressChanged(this, value);
}
}
2013-02-21 01:33:05 +00:00
}
}