Last active
February 15, 2022 14:11
-
-
Save tkojitu/a83edb4c6adc3220e2a0a7b34a2e3f8d to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Timers; | |
using System.Threading; | |
public class Example | |
{ | |
private System.Timers.Timer aTimer; | |
private int count = 0; | |
private Thread thread = Thread.CurrentThread; | |
public static void Main() | |
{ | |
new Example().Demo(); | |
} | |
private void Demo() | |
{ | |
SetTimer(); | |
try | |
{ | |
Thread.Sleep(1000 * 60); | |
} | |
catch (ThreadInterruptedException e) | |
{ | |
aTimer.Stop(); | |
aTimer.Dispose(); | |
} | |
} | |
private void SetTimer() | |
{ | |
aTimer = new System.Timers.Timer(1000); | |
aTimer.Elapsed += OnTimedEvent; | |
aTimer.AutoReset = true; | |
aTimer.Enabled = true; | |
} | |
private void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e) | |
{ | |
++count; | |
if (count >= 5) | |
{ | |
thread.Interrupt(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment