aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Core/Threading/TimeoutTask.cs
blob: 04edaba85cffede31f56990dac4f08782d3a19b3 (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Tango.Core.Threading
{
    /// <summary>
    /// Represents a simple task with a delay mechanism.
    /// </summary>
    public class TimeoutTask
    {
        private Action _action;
        private TimeSpan _timeout;

        /// <summary>
        /// Initializes a new instance of the <see cref="TimeoutTask"/> class.
        /// </summary>
        /// <param name="action">The action.</param>
        /// <param name="timeout">The timeout.</param>
        public TimeoutTask(Action action, TimeSpan timeout)
        {
            _action = action;
            _timeout = timeout;
        }

        /// <summary>
        /// Starts the task after the specified timeout.
        /// </summary>
        public void Start()
        {
            Thread t = new Thread(() => 
            {
                Thread.Sleep(_timeout);
                _action();
            });
            t.IsBackground = true;
            t.Start();
        }

        /// <summary>
        /// Starts a new <see cref="TimeoutTask"/> with the specified timeout.
        /// </summary>
        /// <param name="action">The action.</param>
        /// <param name="timeout">The timeout.</param>
        public static void StartNew(Action action,TimeSpan timeout)
        {
            new TimeoutTask(action, timeout).Start();
        }
    }
}