aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common/Progress/ActionableProgress.cs
blob: 39f46ce0572f5c41adcf14aafb8be20ca9f43068 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System;
using System.Collections.Generic;

namespace MediaBrowser.Common.Progress
{
    /// <summary>
    /// Class ActionableProgress
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ActionableProgress<T> : Progress<T>, IDisposable
    {
        /// <summary>
        /// The _actions
        /// </summary>
        private readonly List<Action<T>> _actions = new List<Action<T>>();

        /// <summary>
        /// Registers the action.
        /// </summary>
        /// <param name="action">The action.</param>
        public void RegisterAction(Action<T> action)
        {
            _actions.Add(action);

            ProgressChanged -= ActionableProgress_ProgressChanged;
            ProgressChanged += ActionableProgress_ProgressChanged;
        }

        /// <summary>
        /// Actionables the progress_ progress changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        void ActionableProgress_ProgressChanged(object sender, T e)
        {
            foreach (var action in _actions)
            {
                action(e);
            }
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
        }

        /// <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)
            {
                ProgressChanged -= ActionableProgress_ProgressChanged;
                _actions.Clear();
            }
        }
    }
}