简体   繁体   English

具有连续(无尽)任务的线程管理控制台程序

[英]Thread management console program with continuous (endless) tasks

I'm new to threading so have some patience please. 我是线程技术的新手,请耐心等待。

I have tens of thousands of rows in a database. 我在数据库中有成千上万的行。 Each row represents a job needed to be done over the internet. 每行代表需要通过Internet完成的一项工作。 I read a data row, I do some network-related work (which can even take between a couple of seconds up to a couple of minutes) and I grab the next data row (my C# application uses console, not GUI). 我读了一个数据行,做了一些与网络有关的工作(甚至可能需要几秒钟到几分钟的时间),然后抓取下一个数据行(我的C#应用​​程序使用控制台,而不是GUI)。 As you might expect I want to do these jobs concurrently. 如您所料,我想同时做这些工作。

I looked into this subject and I thought I would use BackgroundThreads, but if I understand correctly people suggest there is no point in using them in a console application. 我研究了这个主题,并以为会使用BackgroundThreads,但是如果我理解正确, 人们建议在控制台应用程序中使用它们是没有意义的。

I assume I should not use Tasks, because each of my "tasks" will be represented by a single thread. 我假设我不应该使用Tasks,因为我的每个“任务”都将由一个线程表示。

So I thought I would use ThreadPool with regular Threads. 所以我想我将ThreadPool与常规Threads一起使用。

To make things simple I just want to keep a constant number of threads (spawn new ones when one finishes) untill I run out of things to do (then I wait for data - usually alot of it - to arrive in the database and spawn threads). 为了使事情变得简单,我只想保持恒定数量的线程(在一个线程结束时生成新线程),直到我用尽所有要做的事情(然后我等待数据(通常是很多)到达数据库并生成线程) )。 I need to know when a Thread ends because I have to spawn a new thread and update the database row containing data it was working with. 我需要知道线程何时结束,因为我必须产生一个新线程并更新包含正在使用的数据的数据库行。 To keep threads and database in sync I would probably have to mark database row with some kind of thread id when it is retrieved and then mark the row (success/fail) when thread ends. 为了使线程和数据库保持同步,我可能不得不在检索数据库行时用某种线程ID对其进行标记,然后在线程结束时将该行标记为(成功/失败)。 Is this solution (try catch in thread delegate) enough to be sure that a thread has ended (and if it succeded or threw exception)? 此解决方案 (尝试在线程委托中进行捕获)是否足以确保线程已结束(并且是否成功或引发了异常)?

I am not sure how to "wait" for the first thread to end - not all and not a particular one. 我不确定如何“等待”第一个线程结束-不是所有线程,也不是特定线程。

I also think that I don't want to read too much data in advance (and potentially wait for a thread to free up) because there might be other programs doing the same thing using the same database. 我还认为我不想提前读取太多数据(并可能等待线程释放),因为可能会有其他程序使用相同的数据库执行相同的操作。

Any ideas appreciated! 任何想法表示赞赏!

Just use Parallel.ForEach to do this: 只需使用Parallel.ForEach即可:

Parallel.ForEach(rows, row => ProcessRow(row));

If you need to specify a max degree of parallelization because the automatic partitioner happens to be using too many thread pool threads then you can specify it like so: 如果由于自动分区器恰好使用了太多线程池线程而需要指定最大并行度,则可以这样指定:

Parallel.ForEach(rows, new ParallelOptions() { MaxDegreeOfParallelism = 5 }
    , row => ProcessRow(row));

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

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