多个线程填满了他们的结果在一个DataTable中C# [英] Multiple threads filling up their result in one DataTable C#

查看:183
本文介绍了多个线程填满了他们的结果在一个DataTable中C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始学习线程的概念,我有点停留在这一个问题,它让我疯了....

I'm just beginning to learn the concept of threading, and I'm kind of stuck at this one problem, its driving me crazy....

我真的需要完成 -

What I actually need to accomplish -

我在本地目录中的一些300的文本文件,这需要分析具体值...后,我发现这些价值观在每一个文本文件,我需要将它们存储在数据库中..
所以我也跟着访问每个文本文件在目录中平原的方法 - 分析和更新所产生的值作为行到本地数据表,以及当我做了解析所有的文件和存储300行到DataTable,我会做的DataTable中的一个SqlBulkCopy的到我的数据库。 !这种方式工作得很好,只是我花大约10分钟来运行我的代码

I have some 300 text files in a local directory, that need to be parsed for specific values... After I find these "values" in each text file, I need to store them in a database.. So I followed the plain approach of accessing each text file in the directory - parsing and updating the resulting values as a row to a local DataTable, and when I'm done parsing all the files and storing 300 rows to the DataTable, I would do a SQLBulkCopy of DataTable to my database. This approach works fine except that it takes me about 10 minutes to run my code!

我正在试图现在要做的 -

What I'm attempting to do now -

创建的每个文件一个新的线程,并保持低于4的线程数在任何特定时间...然后每个线程将通过文件解析并返回一个行更新本地数据表

Create a new thread for each file and keep the thread count below 4 at any given time... then each thread would parse through the file and return a row to update the local DataTable

在哪里我坚持 - 我不明白如何更新此DataTable表,从多个线程获取行...

Where I'm stuck - I don't understand how to update this single Datatable that gets rows from multiple threads...

相当的解释是不是..希望这里有人可以推荐一个好主意,这个...

Quite an explanation isn't it.. hope some one here can suggest a good idea for this...

谢谢,
尼迪

推荐答案

这将是,如果你只是让每个四个线程的写入数据库本身容易得多。在这种情况下,你不必担心线程(除了那些文件每个线程的工作),因为每个工作线程可以保持自己的数据表和消费中的文件的25%。

This will be much easier if you just let each of your four threads write to the database themselves. In this scenario you don't have to worry about threading (except for what files each thread works on) as each worker thread could maintain their own datatable and consume 25% of the files.

另外,你可以拥有所有的线程使用一个DataTable - 只要确保包裹访问它像这样的锁:

Alternatively, you can have a single datatable that all the threads use--just make sure to wrap accesses to it with a lock like so:

lock(YourTable.Rows.SyncRoot){
  // add rows to table
}


当然,如果瓶颈是磁盘,如@大卫b注意这一切都是没有实际意义。

Of course this is all moot if the bottleneck is the disk, as @David B notes.

这篇关于多个线程填满了他们的结果在一个DataTable中C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆