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 8376099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:20:48+00:00 2026-06-09T15:20:48+00:00

i often populate data reader with data and populate UI like this way using

  • 0

i often populate data reader with data and populate UI like this way

using (SqlConnection conn = new SqlConnection("myConnString"))
using (SqlCommand comm = new SqlCommand("Select * from employee where salary<5000", conn))
{
    conn.Open();

    SqlDataReader reader = comm.ExecuteReader();

    if (reader.HasRows)
    {
        while (reader.Read())
        {
            // here i populate my employee class
        }
    }
    // here i update UI
}

i was searching for the use of Task Parallel library with DataReader and found piece of code. it looks nice but objective is not very clear to me. so here is the code i got.

public IEnumerable<MyDataClass> ReadData()
{
using (SqlConnection conn = new SqlConnection("myConnString"))
using (SqlCommand comm = new SqlCommand("myQuery", conn))
{
    conn.Open();

    SqlDataReader reader = comm.ExecuteReader();

    if (reader.HasRows)
    {
        while (reader.Read())
        {
            yield return new MyDataClass(... data from reader ...);
        }
    }
}
}

calling like

Parallel.ForEach(this.ReadData(), data =>
{
// Use the data here...
});

OR

this.ReadData().AsParallel().ForAll(data => 
{
// Use the data here...
});

how could i get the data from ForAll.

can anyone help me to understand the code snippet that how it works and how to get data from ForAll and how can i populate my UI from ForAll.

another question that how do i know that which class is thread safe or not. what does it mean thread safe. a person said datareader is not thread safe. how he knows.

another question when one should use task parallel library.
please guide. thanks

  • 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-06-09T15:20:49+00:00Added an answer on June 9, 2026 at 3:20 pm

    You can find information about thread-safety of every type in the .NET base class library in the MSDN documentation. Most types are not thread-safe. SqlDataReader for instance, is not thread-safe, since it works on a single connection to the database.

    However, Parallel.ForEach is a very clearer construct. You can’t really iterate an IEnumerable with multiple thread simultaneously, and Parallel.ForEach doesn’t do that. Although it spins up multiple threads and those multiple threads do iterate on the given IEnumerable, Parallel.ForEach ensures that only one thread at the time iterates the enumerable’s IEnumerator. It operates on the assumption that processing elements takes more time than getting the items from the enumerable. Iterating the enumerable is a sequential operation.

    This means that even if the underlying data source and the use of the SqlReader is not thread-safe, you can still process the items in parallel using the Parallel.ForEach. Unfortunately, the MSDN documentation isn’t very explicit about this, but it has to be, since IEnumerator instances returned from GetEnumerator() methods are never thread-safe.

    Still you have to make sure that the given Action<T> is thread-safe, of course.

    You can see this behavior, using the following program:

    public static IEnumerable<int> GetNumbers()
    {
        for (int i = 0; i < 140; i++)
        {
            Console.WriteLine(
                "                          Enumerating " + 
                i + " at thread " +
                Thread.CurrentThread.ManagedThreadId);
    
            yield return i;
        }
    }
    
    static void Main(string[] args)
    {
        Console.ReadLine();
    
        Parallel.ForEach(GetNumbers(), number =>
        {
            Console.WriteLine("Processing " + number + 
                " at thread " +
                Thread.CurrentThread.ManagedThreadId);
    
            Thread.Sleep(1);
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Often times I write some SQL like this: string sql = @ -- Multi-line
Often when writing PHP I'll have it output some HTML like this - echo
As we all know most apps have a data access layer, often using repository
My data access classes often have a method called loadDataToEntity or something similar. This
Often I create Child threads within the main() as Thread thread = new Thread(new
Often, I would like to build up complex regexps from simpler ones. The only
I want to do something like this in my domain/entity object : @Entity @NamedQueries({
I have an MVC application that I'm using various JsonResult endpoints to populate the
I have a form in my forms.py that looks like this: from django import
I'm new to GWT and also fairly new to doing Java web development, using

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.