Hm .. What do you need in this case: One thread calls DoThis for some time in a row. Can another start DoThat at least t2 seconds after the LAST call to DoThat or the first after the last call to DoThat?
I think that if your target platform is Win, then it is better to use WaitableTimer (although it is not implemented in .NET, but you can use it through the API. You need to define these functions:
[DllImport("kernel32.dll")] public static extern IntPtr CreateWaitableTimer(IntPtr lpTimerAttributes, bool bManualReset, string lpTimerName); [DllImport("kernel32.dll")] public static extern bool SetWaitableTimer(IntPtr hTimer, [In] ref long pDueTime, int lPeriod, IntPtr pfnCompletionRoutine, IntPtr lpArgToCompletionRoutine, bool fResume); [DllImport("kernel32", SetLastError = true, ExactSpelling = true)] public static extern Int32 WaitForSingleObject(IntPtr handle, int milliseconds); public static uint INFINITE = 0xFFFFFFFF;
And then using this as follows:
private IntPtr _timer = null; //Before first call of DoThis or DoThat you need to create timer: //_timer = CreateWaitableTimer (IntPtr.Zero, true, null); public static void DoThis() { //Waiting until timer signaled WaitForSingleObject (_timer, INFINITE); DoStuff(); long dueTime = 10000 * 1000 * seconds; //dueTime is in 100 nanoseconds //Timer will signal once after expiration of dueTime SetWaitableTimer (_timer, ref dueTime, 0, IntPtr.Zero, IntPtr.Zero, false); } public static void DoThis() { //Waiting until timer signaled WaitForSingleObject (_timer, INFINITE); DoOtherStuff(); long dueTime = 10000 * 1000 * seconds; //dueTime is in 100 nanoseconds //Timer will signal once after expiration of dueTime SetWaitableTimer (_timer, ref dueTime, 0, IntPtr.Zero, IntPtr.Zero, false); }
And after use, you can destroy the timer by calling CloseHandle.