I have a C# program that needs to dispatch a thread every X minutes, but only if the previously dispatched thread (from X minutes) ago is not currently still running.
A plain old Timer alone will not work (because it dispatches an event every X minutes regardless or whether or not the previously dispatched process has finished yet).
The process that’s going to get dispatched varies wildly in the time it takes to perform it’s task – sometimes it might take a second, sometimes it might take several hours. I don’t want to start the process again if it’s still processing from the last time it was started.
Can anyone provide some working C# sample code?
Use a while(True) loop in the thread. Record the start time at the top of the loop, perform the task, get the time again, calculate how much time has elaspsed and compare withv the start time. If this is less than X, convert the difference into ms and Sleep() for it.
No timer, no continual thread create, no synchro, no polling, no chance at all that two task instances will ever run concurrently.