Add 15 seconds timeout to session and login

Add AutoTimeout class for use on login and session requests.
Bug report by GamerCorey7.
This commit is contained in:
ORelio 2015-01-27 20:38:59 +01:00
parent ee406b233e
commit 391eca102c
3 changed files with 72 additions and 23 deletions

View file

@ -0,0 +1,41 @@
using System;
using System.Threading;
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>
/// <param name="attempts">Maximum timeout in milliseconds</param>
/// <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>
/// <param name="attempts">Maximum timeout</param>
/// <returns>True if the action finished whithout timing out</returns>
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.Abort();
return success;
}
}
}