Created
January 5, 2017 05:18
-
-
Save enif-lee/f9a7e912f7abe5e535aa0a163ad94f66 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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