using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace MinecraftClient
{
///
/// Allow easy timeout on pieces of code
///
///
/// By ORelio - (c) 2014 - CDDL 1.0
///
public class AutoTimeout
{
///
/// Perform the specified action with specified timeout
///
/// Action to run
/// Maximum timeout in milliseconds
/// True if the action finished whithout timing out
public static bool Perform(Action action, int timeout)
{
return Perform(action, TimeSpan.FromMilliseconds(timeout));
}
///
/// Perform the specified action with specified timeout
///
/// Action to run
/// Maximum timeout
/// True if the action finished whithout timing out
public static bool Perform(Action action, TimeSpan timeout) {
Thread thread = new Thread(new ThreadStart(action));
thread.Start();
bool success = thread.Join(timeout);
if (!success)
thread.Interrupt();
thread = null;
return success;
}
}
}