Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7032163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:53:55+00:00 2026-05-28T00:53:55+00:00

This is the scenario: In a separate task I read from a datareader which

  • 0

This is the scenario: In a separate task I read from a datareader which represent a single column result set with a string, a JSON. In that task I add the JSON string to a BlockingCollection that wraps the ConcurrentQueue. At the same time in the main thread I TryTake/dequeue a JSON string from the collection and then yield return it deserialized.

The reading from the database and the deserialization is approximately of the same speed so there will not be to much memory consumption caused by a large BlockingCollection.

When the reading from the database is done, the task is closed and I then deserialize all the non deserialized JSON strings.

Questions/thoughts:

1) Does the TryTake lock so that no adding can be done?

2) Don’t do it. Just do it in serial and yield return.

using (var q = new BlockingCollection<string>())
{
Task task = null;

try
{
    task = new Task(() =>
    {
        foreach (var json in sourceData)
            q.Add(json);
    });

    task.Start();

    while (!task.IsCompleted)
    {
        string json;
        if (q.TryTake(out json))
            yield return Deserialize<T>(json);
    }

    Task.WaitAll(task);
}
finally 
{
    if (task != null)
    {
        task.Dispose();
    }

    q.CompleteAdding();
}

foreach (var e in q.GetConsumingEnumerable())
    yield return Deserialize<T>(e);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:53:56+00:00Added an answer on May 28, 2026 at 12:53 am

    Question 1

    Does the TryTake lock so that no adding can be done

    There will be a very brief period whereby an add cannot be performed, however this time will be negligible. From http://msdn.microsoft.com/en-us/library/dd997305.aspx

    Some of the concurrent collection types use lightweight
    synchronization mechanisms such as SpinLock, SpinWait, SemaphoreSlim,
    and CountdownEvent, which are new in the .NET Framework 4. These
    synchronization types typically use busy spinning for brief periods
    before they put the thread into a true Wait state. When wait times are
    expected to be very short, spinning is far less computationally
    expensive than waiting, which involves an expensive kernel transition.
    For collection classes that use spinning, this efficiency means that
    multiple threads can add and remove items at a very high rate. For
    more information about spinning vs. blocking, see SpinLock and
    SpinWait.

    The ConcurrentQueue and ConcurrentStack classes do not use locks
    at all. Instead, they rely on Interlocked operations to achieve
    thread-safety.

    Question 2:

    Don’t do it. Just do it in serial and yield return.

    This seems like the way to go. As with any optimisation work – do what is simplest and then measure! If there is a bottleneck here consider optimising, but at least you’ll know if your ‘optimistations’ are actually helping by virtue of having metrics to compare against.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

we have this scenario: A server which contains needed data and client component which
This scenario comes up often, now that I use LinkToSql and the classes it
Consider this scenario: I've an XML file called person.xml with the following data in
Consider this scenario. I have my own website, that I use as my identifier,
Consider this scenario. I have an object, lets call it.... Foo. Foo raises a
Is this scenario even possible? class Base { int someBaseMemer; }; template<class T> class
Imagine this scenario: You have a desktop and a laptop. The desktop has a
How would you test this scenario? I've just started looking into NHibernate and having
I have this scenario where I need data integrity in the physical database. For
The architecture for this scenario is as follows: I have a table of items

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.