private void DelayFunctionWithLock(object lockObj, int delay, int timeout) { Lock.CompletesIn(lockObj, timeout, () => { DelayFunction(delay); }); }
private bool DelayFunctionWithMultipleThreads(int delay, int timeout) { int numThreadsInLockSection = 0; bool concurrentThreadsLessThanTwo = true; object lockObj = new object(); List <Thread> listThread = new List <Thread>(); for (int i = 0; i < NUM_THREADS; i++) { Thread thread = new Thread(() => { Lock.CompletesIn(lockObj, timeout, () => { numThreadsInLockSection++; concurrentThreadsLessThanTwo &= numThreadsInLockSection == 1; DelayFunction(delay); numThreadsInLockSection--; }); }); thread.Start(); listThread.Add(thread); } WaitJoinAllThreads(listThread); return(concurrentThreadsLessThanTwo); }