繁体   English   中英

如何使用DataReader的Task Parallel库

[英]How to work with Task Parallel library with DataReader

我经常用数据填充数据阅读器并以这种方式填充UI

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
}

我正在寻找使用DataReader的任务并行库并找到一段代码。 它看起来不错,但对我来说目标不是很明确。 所以这是我得到的代码。

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 ...);
        }
    }
}
}

打电话给

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

要么

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

我怎样才能从ForAll获取数据。

谁能帮助我理解的代码片段,我如何工作的,以及如何从获得的ForAll数据和如何填充从我的ForAll UI。

另一个问题是我怎么知道哪个类是线程安全的。 什么是线程安全的意思。 一个人说datareader不是线程安全的。 他怎么知道的。

一个人应该使用任务并行库的另一个问题。 请指导。 谢谢

您可以在MSDN文档中找到有关.NET基类库中每种类型的线程安全性的信息。 大多数类型不是线程安全的。 例如, SqlDataReader 不是线程安全的,因为它适用于与数据库的单个连接。

但是, Parallel.ForEach是一个非常清晰的构造。 你不能真正迭代同时使用多个线程的IEnumerable ,而Parallel.ForEach不会这样做。 虽然它会旋转多个线程并且那些多个线程会在给定的IEnumerable上进行迭代,但Parallel.ForEach确保当时只有一个线程迭代可枚举的IEnumerator 它假设处理元素比从枚举中获取项目花费更多时间。 迭代可枚举是一个顺序操作。

这意味着即使底层数据源和SqlReader的使用不是线程安全的,您仍然可以使用Parallel.ForEach处理项目。 遗憾的是,MSDN文档对此并不十分明确,但必须如此,因为从GetEnumerator()方法返回的IEnumerator实例从不是线程安全的。

当然,你必须确保给定的Action<T>是线程安全的。

您可以使用以下程序查看此行为:

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);
    });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM