This question is possibly a duplicate of How do you add a timer to a C# console application and few other similar questions but couldn't find the answer I'm looking for so asking again.
Question: How do you pass data from the Elapsed
event of a Timer (System.Timers.Timer
) to the thread that created the timer (that thread may not be the Main
thread but another thread spawned by Main
)?
I assume there could be some trivial way of achieving this eg. like the BackgroundWorker
ProgressChanged
event being called in the thread that created the worker, but couldn't find a way in MSDN documentation or SO. Most examples I've seen do some action in the timer thread (https://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx) but don't pass anything to the original thread. Needless to say I'm pretty new to C#/.NET so a solution + pointers to references are appreciated.
Edit: I'd prefer not to use the Invoke()
/InvokeRequired
pattern (cf. How to use safe threading for a timer(Change timer properties from different thread) ) as this is not for a Forms application. I'm tempted to solve this by creating a BackgroundWorker
that reports to the original thread at intervals (DoWork
will be just a sleep()
inside a loop) but thought .NET might have this already and hence the question.