2015-01-27 20:38:59 +01:00
|
|
|
|
using System;
|
2022-07-03 22:34:07 +08:00
|
|
|
|
using System.Diagnostics;
|
2015-01-27 20:38:59 +01:00
|
|
|
|
using System.Threading;
|
2022-07-03 22:34:07 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2015-01-27 20:38:59 +01:00
|
|
|
|
|
|
|
|
|
|
namespace MinecraftClient
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Allow easy timeout on pieces of code
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// By ORelio - (c) 2014 - CDDL 1.0
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
public class AutoTimeout
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Perform the specified action with specified timeout
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="action">Action to run</param>
|
2015-01-31 11:21:06 +01:00
|
|
|
|
/// <param name="timeout">Maximum timeout in milliseconds</param>
|
2015-01-27 20:38:59 +01:00
|
|
|
|
/// <returns>True if the action finished whithout timing out</returns>
|
|
|
|
|
|
public static bool Perform(Action action, int timeout)
|
|
|
|
|
|
{
|
|
|
|
|
|
return Perform(action, TimeSpan.FromMilliseconds(timeout));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Perform the specified action with specified timeout
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="action">Action to run</param>
|
2015-01-31 11:21:06 +01:00
|
|
|
|
/// <param name="timeout">Maximum timeout</param>
|
2015-01-27 20:38:59 +01:00
|
|
|
|
/// <returns>True if the action finished whithout timing out</returns>
|
2022-07-03 22:34:07 +08:00
|
|
|
|
public static bool Perform(Action action, TimeSpan timeout) {
|
2015-01-27 20:38:59 +01:00
|
|
|
|
Thread thread = new Thread(new ThreadStart(action));
|
|
|
|
|
|
thread.Start();
|
|
|
|
|
|
bool success = thread.Join(timeout);
|
|
|
|
|
|
if (!success)
|
2022-07-03 22:34:07 +08:00
|
|
|
|
thread.Interrupt();
|
|
|
|
|
|
|
|
|
|
|
|
thread = null;
|
2015-01-27 20:38:59 +01:00
|
|
|
|
return success;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|