using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Tango.Core.Threading
{
///
/// Represents a simple task with a delay mechanism.
///
public class TimeoutTask
{
private Action _action;
private TimeSpan _timeout;
///
/// Initializes a new instance of the class.
///
/// The action.
/// The timeout.
public TimeoutTask(Action action, TimeSpan timeout)
{
_action = action;
_timeout = timeout;
}
///
/// Starts the task after the specified timeout.
///
public void Start()
{
Thread t = new Thread(() =>
{
Thread.Sleep(_timeout);
_action();
});
t.IsBackground = true;
t.Start();
}
///
/// Starts a new with the specified timeout.
///
/// The action.
/// The timeout.
public static void StartNew(Action action,TimeSpan timeout)
{
new TimeoutTask(action, timeout).Start();
}
}
}