summaryrefslogtreecommitdiffstats
path: root/win/CS/HandBrakeWPF/Utilities/DelayedActionProcessor.cs
blob: 4da6f598f646713e1d8178d99c6f7f22926fa15a (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
64
65
66
67
68
69
70
71
72
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DelayedActionProcessor.cs" company="HandBrake Project (http://handbrake.fr)">
//   This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
// </copyright>
// <summary>
//   An Action processor that supports queueing/delayed action processing.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace HandBrakeWPF.Utilities
{
    using System;
    using System.Timers;

    /// <summary>
    /// An Action processor that supports queueing/delayed action processing.
    /// </summary>
    public class DelayedActionProcessor
    {
        /// <summary>
        /// The task.
        /// </summary>
        private Action task;

        /// <summary>
        /// The timer.
        /// </summary>
        private Timer timer;

        /// <summary>
        /// The set task.
        /// </summary>
        /// <param name="taskReset">
        /// The task reset.
        /// </param>
        /// <param name="timems">
        /// The timems.
        /// </param>
        public void PerformTask(Action taskReset, int timems)
        {
            if (timer != null)
            {
                timer.Stop();
                timer.Close();                
            }

            timer = new Timer(timems) { AutoReset = true };
            timer.Elapsed += this.timer_Elapsed;
            task = taskReset;
            timer.Stop();
            timer.Start();
        }

        /// <summary>
        /// The timer_ elapsed.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            if (task != null)
            {
                timer.Stop();
                task();      
            }
        }
    }
}