多个线程将其结果填充在一个DataTable C# [英] Multiple threads filling up their result in one DataTable C#

查看:912
本文介绍了多个线程将其结果填充在一个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个文本文件,需要针对特定​​的值进行解析...在我找到这些 在每个文本文件中,我需要将它们存储在一个数据库中。
所以我遵循访问目录中每个文本文件的简单方法 - 将结果值作为行解析和更新到本地的DataTable,当我完成解析所有文件并将300行存储到DataTable时,我将对我的数据库执行一个DataBable的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以下,然后每个线程将解析该文件并返回一行以更新本地DataTable

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...

谢谢,
Nidhi

Thanks, Nidhi

推荐答案

如果你只要让你的四个线程自己写入数据库,这将会更容易一些。在这种情况下,您不必担心线程(除了每个线程所在的文件),因为每个工作线程都可以维护自己的datatable并消耗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
}






如果@David B注意到,如果瓶颈是磁盘,那么这一切都是愚蠢的。


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

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

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