I often populate data with data and populate the UI 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())
{
}
}
}
I searched for using a parallel task library with a DataReader and found a code snippet. It looks beautiful, but objectively I do not really understand. 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 ...);
}
}
}
}
call
Parallel.ForEach(this.ReadData(), data =>
{
});
OR
this.ReadData().AsParallel().ForAll(data =>
{
});
How can I get data from ForAll .
can someone help me understand a piece of code on how it works and how to get data from ForAll and how I can populate my user interface from ForAll .
another question is how to find out which class is thread safe or not. what does it mean thread safety. The person said that the datareader is not thread safe. as he knows.
, .
, .