-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
1,340 additions
and
288 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.Threading; | ||
|
||
namespace TopMostFriend { | ||
public class ActionTimeout { | ||
private readonly Action Action; | ||
private bool Continue = true; | ||
private int Remaining = 0; | ||
private const int STEP = 500; | ||
|
||
public ActionTimeout(Action action, int timeout) { | ||
Action = action ?? throw new ArgumentNullException(nameof(action)); | ||
if(timeout < 1) | ||
throw new ArgumentException(@"Timeout must be a positive integer.", nameof(timeout)); | ||
Remaining = timeout; | ||
new Thread(ThreadBody) { IsBackground = true }.Start(); | ||
} | ||
|
||
private void ThreadBody() { | ||
do { | ||
Thread.Sleep(STEP); | ||
Remaining -= STEP; | ||
|
||
if(!Continue) | ||
return; | ||
} while(Remaining > 0); | ||
|
||
Action.Invoke(); | ||
} | ||
|
||
public void Cancel() { | ||
Continue = false; | ||
} | ||
} | ||
} |
This file contains 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
Oops, something went wrong.