So what do you think is the best way to prevent multiple threads of a C# Windows service running simultaneously (the service is using a timer
with the OnE
I think I know what you're trying to do. You've got a timer that executes a callback periodically (definition of a timer) and that callback does a bit of work. that bit of work could actually take more time than the timer period (e.g. the timer period is 500 ms and a given invocation of your callback could take longer that 500 ms). This means that your callback needs to be re-entrant.
If you can't be re-entrant (and there's various reasons why this might be); what I've done in the past is to turn off the timer at the start of the callback then turn it back on at the end. For example:
private void timer_Elapsed(object source, ElapsedEventArgs e)
{
timer.Enabled = false;
//... do work
timer.Enabled = true;
}
If you want to actually want one "thread" to execute immediately after another, I wouldn't suggest using a timer; I would suggest using Task objects. For example
Task.Factory.StartNew(()=>{
// do some work
})
.ContinueWith(t=>{
// do some more work without running at the same time as the previous
});