Skip to content

Instantly share code, notes, and snippets.

@tkojitu
Last active February 15, 2022 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tkojitu/a83edb4c6adc3220e2a0a7b34a2e3f8d to your computer and use it in GitHub Desktop.
Save tkojitu/a83edb4c6adc3220e2a0a7b34a2e3f8d to your computer and use it in GitHub Desktop.
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