A System.Theading.Timer example that waits for a long running process to finish
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.Threading; | |
public class TimerTest : IDisposable | |
{ | |
private readonly Timer _timer; | |
private readonly object _locker = new object(); | |
public TimerTest() | |
{ | |
_timer = new Timer(new TimerCallback(TickTimer), null, 1000, 1000); | |
} | |
private void TickTimer(object state) | |
{ | |
// keep GC from collecting and stopping timer; | |
GC.KeepAlive(_timer); | |
// do not let another tick happen if we are still doing things - https://stackoverflow.com/a/13267259/2041 | |
if (Monitor.TryEnter(_locker)) | |
{ | |
try | |
{ | |
// do long running work here | |
DoWork(); | |
} | |
finally | |
{ | |
Monitor.Exit(_locker); | |
} | |
} | |
} | |
private void DoWork() | |
{ | |
Console.WriteLine("Starting DoWork()"); | |
Thread.Sleep(2000); | |
Console.WriteLine("Ending DoWork()"); | |
} | |
public void Dispose() | |
{ | |
_timer?.Dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment