Permalink
Browse files

fix issue with dot3.5 BlockingCollection

  • Loading branch information...
somdoron committed Oct 24, 2015
1 parent 91c4e69 commit 5c838f3d30d483dcadb4181233a4437fb5e7f327
Showing with 8 additions and 17 deletions.
  1. +8 −17 Source/AsyncIO/BlockingCollection.cs
@@ -30,37 +30,28 @@ public void Add(T item)
public bool TryTake(out T item, int timeout)
{
- Stopwatch stopwatch = Stopwatch.StartNew();
+ Stopwatch stopwatch = Stopwatch.StartNew();
- int timeoutLeft = timeout == -1 ? -1 :
- (stopwatch.ElapsedMilliseconds > timeout ? 0 : timeout - (int)stopwatch.ElapsedMilliseconds);
-
- if (Monitor.TryEnter(m_queue, timeoutLeft))
+ lock (m_queue)
{
while (m_queue.Count == 0)
{
- timeoutLeft = timeout == -1 ? -1 :
- (stopwatch.ElapsedMilliseconds > timeout ? 0 : timeout - (int)stopwatch.ElapsedMilliseconds);
+ long elapsed = stopwatch.ElapsedMilliseconds;
+ int timeoutLeft = timeout == -1 ? -1 :
+ (elapsed > timeout ? 0 : timeout - (int)elapsed);
if (timeoutLeft == 0)
{
- item = default(T);
- Monitor.Exit(m_queue);
+ item = default(T);
return false;
}
Monitor.Wait(m_queue, timeoutLeft);
}
- item = m_queue.Dequeue();
- Monitor.Exit(m_queue);
+ item = m_queue.Dequeue();
return true;
- }
- else
- {
- item = default(T);
- return false;
- }
+ }
}
}
}

0 comments on commit 5c838f3

Please sign in to comment.