Esempio n. 1
0
 /// <summary>
 /// Blocks if no release is signaled or removes a release signal. Enter a critical section.
 /// In theory, this method is often called P() [Dijkstra, dutch: passeeren]
 /// </summary>
 /// <remarks>This member is thread-safe.</remarks>
 public void Acquire()
 {
     while (true)
     {             //looped 0.5s timeout to make blocked threads abortable.
         uint res = NTKernel.WaitForSingleObject(handle, interruptReactionTime);
         try     { System.Threading.Thread.Sleep(0); }
         catch (System.Threading.ThreadInterruptedException e)
         {
             if (res == 0)
             {
                 int previousCount;
                 NTKernel.ReleaseSemaphore(handle, 1, out previousCount);
             }
             throw e;
         }
         if (res == 0)
         {
             return;
         }
         if (res != 258)
         {
             throw new SemaphoreFailedException();
         }
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Blocks if no release is signaled or removes a release signal. Enter a critical section.
        /// In theory, this method is often called P() [Dijkstra, dutch: passeeren]
        /// </summary>
        /// <param name="timeout">The maximum blocking time. Usually an Exceptions is thrown if a timeout exceeds.</param>
        /// <remarks>This member is thread-safe.</remarks>
        public void Acquire(TimeSpan timeout)
        {
            uint milliseconds = (uint)timeout.TotalMilliseconds;

            if (NTKernel.WaitForSingleObject(handle, milliseconds) != 0)
            {
                throw new SemaphoreFailedException();
            }
        }