title: How to work with Task Parallel library with DataReader
body: <p>i often populate data reader with data and populate UI like this way</p>
  
  <pre><code>using (SqlConnection conn = new SqlConnection("myConnString"))
  using (SqlCommand comm = new SqlCommand("Select * from employee where salary&lt;5000", conn))
  {
      conn.Open();
  
      SqlDataReader reader = comm.ExecuteReader();
  
      if (reader.HasRows)
      {
          while (reader.Read())
          {
              // here i populate my employee class
          }
      }
      // here i update UI
  }
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>public IEnumerable&lt;MyDataClass&gt; 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 ...);
          }
      }
  }
  }
  </code></pre>
  
  <p><strong>calling like</strong> </p>
  
  <pre><code>Parallel.ForEach(this.ReadData(), data =&gt;
  {
  // Use the data here...
  });
  </code></pre>
  
  <p>OR</p>
  
  <pre><code>this.ReadData().AsParallel().ForAll(data =&gt; 
  {
  // Use the data here...
  });
  </code></pre>
  
  <p>how could i get the data from <strong>ForAll</strong>.</p>
  
  <p>can anyone help me to understand the code snippet that how it works and how to get data from <strong>ForAll</strong> and how can i populate my UI from <strong>ForAll</strong>.</p>
  
  <p>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.</p>
  
  <p>another question when one should use task parallel library.
  please guide. thanks</p>