Giter VIP home page Giter VIP logo

Comments (3)

StephenCleary avatar StephenCleary commented on May 21, 2024 1

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, since TryTake executes immediately. So the semantics of AsyncCollection<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 the Try 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 like BlockingCollection<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 of AsyncCollection<T>.TryTakeAsync, which has very little in common with the semantics of BlockingCollection<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.

StephenCleary avatar StephenCleary commented on May 21, 2024

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.

eKipod avatar eKipod commented on May 21, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.