I am trying to create a function that takes in an Action and a Timeout, and executes the Action after the Timeout. The function is to be non-blocking. The function must be thread safe. I also really, really want to avoid Thread.Sleep().
So far, the best I can do is this:
long currentKey = 0;
ConcurrentDictionary<long, Timer> timers = new ConcurrentDictionary<long, Timer>();
protected void Execute(Action action, int timeout_ms)
{
long currentKey = Interlocked.Increment(ref currentKey);
Timer t = new Timer(
(key) =>
{
action();
Timer lTimer;
if(timers.TryRemove((long)key, out lTimer))
{
lTimer.Dispose();
}
}, currentKey, Timeout.Infinite, Timeout.Infinite
);
timers[currentKey] = t;
t.Change(timeout_ms, Timeout.Infinite);
}
The problem is that calling Dispose() from the callback itself cannot be good. I am unsure if it is safe to "fall off" the end, i.e. Timers are considered live while their lambdas are executing, but even if this is the case I'd rather dispose it properly.
The "fire once with a delay" seems like such a common problem that there should be an easy way to do this, probably some other library in System.Threading I am missing, but right now the only solution I can think of is modification of the above with a dedicated cleanup task running on an interval. Any advice?
Why not simply invoke your action parameter itself in an asynchronous action?
I don't know which version of C# you are using. But I think you could accomplish this by using the Task library. It would then look something like that.
This seems to work for me. It allows me to invoke _connection.Start() after a 15 second delay. The -1 millisecond parameter just says don't repeat.
My example:
Documentation clearly states that System.Timers.Timer has
AutoReset
property made just for what you are asking:https://msdn.microsoft.com/en-us/library/system.timers.timer.autoreset(v=vs.110).aspx
Use Microsoft's Reactive Framework (NuGet "System.Reactive") and then you can do this: