i don't want to sleep the thread, because as i understand it that will also pause the timers on the other worker objects
That is incorrect; it will not stop the timers.
my application is a single thread
No, actually, it's not. The timers will be creating and using other threads to implement their behavior. The Elapsed
event handlers will be fired from a thread pool thread.
One major issue with your code is that your main
method is doing a "busywait" on the three objects, constantly polling them asking if they're done. This is...expensive. It's basically like you're kids asking, "Are we there yet." a few hundred times a second. Wouldn't it be so much nicer if they just sat their waiting until you told them you were done! (That's quite possible, and a good possible option.)
One of the simpler solutions in this case would be to do the loop in the worker, not Main
. Have the implementation of startWorking
wrapped in a while
loop, and have main just do a wait forever (i.e. Thread.Sleep(Timeout.Infinite);
). More complex options would be having the workers provide a Task
, event, or blocking wait (also called a "Join") method to indicate when they are finished.
The option you're considering, that of adding a Thread.Sleep
of a little while in Main
will help, but it's only telling your kids to ask you when you're there less often, rather than having them wait for you to tell them when you're there.