/// <summary> /// Try to de-queue from the Channel with a timeout period. /// </summary> /// <returns> /// Returns Boolean, representing whether or not data was de-queued from the Channel. /// </returns> /// <param name="milliseconds"> /// Specifies the timeout period for de-queueing. /// </param> /// <param name="result"> /// Data that was de-queued from the Channel. /// </param> /// <exception cref="System.Threading.ThreadInterruptedException"> /// Thrown when de-queueing is interrupted. /// </exception> public virtual bool TryDequeue(int milliseconds, out T result) { // Catch cases which mean infinite timeout period if (milliseconds < -1) { milliseconds = -1; } // Can de-queue happen if (_access.TryAcquire(milliseconds)) { try { result = _queue.Dequeue(); return(true); } catch (ThreadInterruptedException) { // Catch the exception if it is thrown and force release then throw _access.ForceRelease(); throw; } } // Otherwise, assign the out variable the value of the default type, T, and return false else { result = default(T); return(false); } }
/// <summary> /// Try to en-queue to the BoundedChannel with a timeout period. /// </summary> /// <returns> /// Returns Boolean, representing whether or not data was en-queued to the BoundedChannel. /// </returns> /// <param name="data"> /// Specifies the data to be en-queued to the BoundedChannel. /// </param> /// <param name="milliseconds"> /// Specifies the timeout period for en-queueing. /// </param> /// <exception cref="System.Threading.ThreadInterruptedException"> /// Thrown when en-queueing is interrupted. /// </exception> public bool TryEnqueue(T data, int milliseconds) { // Catch cases which mean infinite timeout period if (milliseconds < -1) { milliseconds = -1; } // If a token has successfully been acquired from the BoundedChannel's boundary Semaphore, en-queue the data with base.Enqueue and assign bool true if (_upperBoundary.TryAcquire(milliseconds)) { try { // An interrupt may occure here base.Enqueue(data); } catch (ThreadInterruptedException) { // Catch the interrupt, force release into the boundary and throw the caught exception _upperBoundary.ForceRelease(); throw; } return(true); } return(false); }
private static void SemaphoreTestAcquire() { switch (new Random().Next(2)) { case 0: { int maxWaitTime = new Random().Next(4000); Console.WriteLine(Thread.CurrentThread.Name + ": I'm going to wait " + maxWaitTime + " milliseconds to acquire."); try { if (_testSemaphore.TryAcquire(maxWaitTime)) { Console.WriteLine("\n\t" + Thread.CurrentThread.Name + ": I managed to acquire!\n"); } else { Console.WriteLine("\t" + Thread.CurrentThread.Name + ": I've waited long enough."); Console.WriteLine("\t" + "\t" + Thread.CurrentThread.Name + ": Now let's get back to acquiring."); } } catch (ThreadInterruptedException) { Console.WriteLine("\n\t" + Thread.CurrentThread.Name + ": I was interrupted!\n"); } break; } case 1: { Console.WriteLine(Thread.CurrentThread.Name + ": I'm going to wait infinitely to acquire."); _requiresInterrupt = true; try { if (_testSemaphore.TryAcquire(-1)) { Console.WriteLine("\n\t" + Thread.CurrentThread.Name + ": I managed to acquire!\n"); } } catch (ThreadInterruptedException) { Console.WriteLine("\n\t" + Thread.CurrentThread.Name + ": I was interrupted!\n"); } break; } } }
private static void SemaphoreTestAcquire() { int maxWaitTime = new Random().Next(4000); Console.WriteLine(Thread.CurrentThread.Name + ": I'm going to wait " + maxWaitTime + " milliseconds to acquire."); if (_testSemaphore.TryAcquire(maxWaitTime)) { Console.WriteLine("\n\t" + Thread.CurrentThread.Name + ": I managed to acquire!\n"); } else { Console.WriteLine("\t" + Thread.CurrentThread.Name + ": I've waited long enough and should be interrupted."); Console.WriteLine("\t" + "\t" + Thread.CurrentThread.Name + ": Now let's get back to acquiring."); } }