Skip to content

Instantly share code, notes, and snippets.

@enif-lee
Created January 5, 2017 05:18
Show Gist options
  • Save enif-lee/f9a7e912f7abe5e535aa0a163ad94f66 to your computer and use it in GitHub Desktop.
Save enif-lee/f9a7e912f7abe5e535aa0a163ad94f66 to your computer and use it in GitHub Desktop.
class TestTimeout
{
static void Main(string[] args)
{
new Timeout(
start: () =>
{
Thread.Sleep(10000);
Console.WriteLine("it's working!");
},
timeouted: () => Console.WriteLine("timed out!"),
milisecond: 1000).Run();
}
class Timeout
{
ThreadStart start;
Action timeouted;
int milisecond;
public Timeout(ThreadStart start, Action timeouted, int milisecond)
{
this.start = start;
this.timeouted = timeouted;
this.milisecond = milisecond;
}
public void Run () {
Thread thread = new Thread(start);
thread.Start();
if (!thread.Join(this.milisecond))
{
timeouted();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment