Comments (3)
I agree the method name is confusing. I am doing a thorough API review as I'm moving the code to the new .NET core platform, and I will definitely be addressing this.
FYI, the historical reason for this name is like this:
- An asynchronous version of
BlockingCollection<T>.TryTake
didn't make much sense, sinceTryTake
executes immediately. So the semantics ofAsyncCollection<T>.TryTakeAsync
are to (asynchronously) wait for the next item (or completion). - While the
Try
prefix can mean "immediately synchronous" (e.g.,Monitor.TryEnter
), I was actually interpreting theTry
prefix as meaning "return false rather than throw" (e.g.,int.TryParse
). Looking back now, it's clear both interpretations are valid:BlockingCollection<T>.TryTake
is just likeBlockingCollection<T>.Take
except that it is immediately synchronous and returns false rather than throws when the collection is empty and completed. - When I added the synchronous APIs some time later, I'm afraid I blindly applied them to all the asynchronous methods. So that's why
AsyncCollection<T>.TryTake
does block; it's literally the blocking version ofAsyncCollection<T>.TryTakeAsync
, which has very little in common with the semantics ofBlockingCollection<T>.TryTake
.
So, it's not an excuse, but at least an explanation. :) And I will address this in the next backwards-incompatible version, which should be in the next few months. I am actually in the process of reviewing all APIs.
from asyncex.
Yes, it is by design. The AsyncCollection API has been updated to include blocking methods as well as asynchronous methods. (This is useful if either your producer or consumer is synchronous).
So, TryTake
is a synchronous method; it will block until there is an element available or the collection is marked CompleteAdding. TryTakeAsync
is the asynchronous equivalent.
from asyncex.
My question may have not been clear. I typically expect Try- functions to be non blocking, i.e. to fail / return false if a lock could not be acquired (essentially, use "TryLock"), and/or if the collection is empty (as is the behaviour of BlockingCollection). Otherwise, how is TryTake different than Take?
from asyncex.
Related Issues (20)
- How to upgrade from v3 to v5? HOT 1
- Enforce that monitor is entered when calling `Pulse` and `PulseAll` or remove requirement? HOT 1
- Could not load type 'Nito.AsyncEx.SynchronizationContextSwitcher' from assembly 'Nito.AsyncEx.Tasks, Version=1.0.1.0, Culture=neutral, PublicKeyToken=null'.":"Nito.AsyncEx.SynchronizationContextSwitcher HOT 4
- Contradiction? HOT 1
- Support AsyncProducerConsumerQueue wait for empty
- AsyncCollection IsEmpty HOT 3
- [Discussion] Return `ValueTask` instead of `AwaitableDisposable`? HOT 5
- [Question] .WaitAndUnwrapException() vs AsyncContext and potential deadlock HOT 2
- The collection has been marked as complete with regards to additions HOT 1
- Atomic wait semantics lead to continued processing after cancellation HOT 3
- What happened to NotifyTask? HOT 1
- CancellationTokenTaskSource's dispose
- Using LazyAsync with JSONSeralizer? HOT 1
- Producer/Consumer PriorityQueue
- doc/TaskConstants.md links to an article that doesn't exist anymore
- Cross thread Sync context stealing HOT 2
- [Question] Using discard with AsyncLock HOT 1
- [Question] Is ConfigureAwait(false) is needed? HOT 1
- AsyncContext holds exceptions until all tasks complete.
- Feature request: AsyncReaderWriterLock support for UpgradeableReadLock HOT 11
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from asyncex.