如何使用具有这些属性的 C# 4.0 任务功能 - 在运行时分配变量 - 保持一定数量的任务处于活动状态

发布于 2024-12-07 09:15:45 字数 850 浏览 0 评论 0原文

好吧,我已经搜索了好几天了,但没有任何例子说明我想要实现的目标。目前我可以使用任务进行多线程爬行,但写得非常糟糕。您可以从这里看到完整的代码:

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/6fa6fd12-85c5-4489-81b1-25bc0126d406

现在是我的问题。我想使用 System.Threading.Tasks.Task 进行多线程处理。开发是microsoft Visual Studio 2010和C# 4.0。

我需要的是

1-) 我想保持一定数量的任务始终运行。因此,当一个线程完成其工作时,另一个线程应该立即开始工作。通过这种方式,总是有一定数量的线程会主动运行。

2-) 我需要能够在运行时提供任务变量。所以想想我有一个链接池。我从前 100 个任务开始,但假设第 45 个任务首先完成。因此将创建另一个任务,该任务将占用第 101 个链接。当另一个任务完成时,它将继续以这种方式进行,并且始终有一定数量的任务处于活动状态。

3-) 我需要能够收集任务结果。任务完成后以某种方式收集结果。这部分似乎是最简单的部分。

无论我发现哪个任务示例都只显示 2 个任务或 3 个任务,某些变量在运行时不会发生变化,并且不会保持某些数字处于活动状态。多线程应用程序是未来,但文档却很糟糕。

Alright i have been searching for days but there is no example of what i am trying to achieve. Currently i am able to use tasks for multi-threaded crawling but it is very bad written. You can see the whole code from here :

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/6fa6fd12-85c5-4489-81b1-25bc0126d406

Now coming my question. I want to use System.Threading.Tasks.Task for multi-threading. Development is microsoft visual studio 2010 and C# 4.0.

What i need is

1-) I want to keep certain number of tasks running for all the time. So when a thread finished its job another thread should start working immediately. With this way always certain number of threads will be actively running.

2-) I need to be able to give tasks variables on run time. So think as i have a links pool. I started with first 100 but lets say 45 th task finished first. So another task will be created and this task will take number 101 th link. When another one finished it will continue as this way and always certain number of tasks will be alive.

3-) I need to be able to collect results of tasks. After a task finished somehow collect the result. This part seems like the easiest part.

Whichever task example i found just shows 2 task or 3 task , getting certain variables not changing on run time and not keeping certain numbers alive. Multi-threaded applications are the future but there is so bad documentation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清眉祭 2024-12-14 09:15:45

您是否已研究过 TPL-DataFlow CPL:

TPL Dataflow (TDF) 是一个新的 .NET 库,用于构建并发
应用程序。它通过以下方式促进以演员/代理为导向的设计
用于进程内消息传递、数据流和管道的原语。
TDF 建立在 API 和调度基础设施的基础上
.NET 4 中的任务并行库 (TPL),并与
C#、Visual Basic 和 F# 提供的异步语言支持。

这是测试版,但我认为你的问题不是线程。
似乎您正在尝试构建某种基于代理的编程模型,并且此 DataFlow 为您提供了一些非常好的工具。

这是主页,这是一个不错的视频

Have you looked into the TPL-DataFlow CPL yet:

TPL Dataflow (TDF) is a new .NET library for building concurrent
applications. It promotes actor/agent-oriented designs through
primitives for in-process message passing, dataflow, and pipelining.
TDF builds upon the APIs and scheduling infrastructure provided by the
Task Parallel Library (TPL) in .NET 4, and integrates with the
language support for asynchrony provided by C#, Visual Basic, and F#.

It's beta but I think your problem are not the threads.
Seems you are trying to build some kind of Agent-based programming model and this DataFlow gives you some really nice tools for this.

Here is the Homepage and here is a nice video on this.

ぽ尐不点ル 2024-12-14 09:15:45

为什么需要一定数量的任务?让池/API 决定。

将数据传递给任务就像将数据传递给线程一样:共享数据结构。

使用任务返回任务结果

编辑

您无法设置工作线程数或 I/O 数
完成线程数小于处理器数
在计算机中。

如果公共语言运行时是托管的,例如通过 Internet
信息服务 (IIS) 或 SQL Server,主机可以限制或
防止更改线程池大小。

更改线程中的最大线程数时要小心
水池。虽然您的代码可能会受益,但这些更改可能会产生不利影响
对您使用的代码库的影响。

将线程池大小设置得太大可能会导致性能问题。
如果同时执行的线程过多,则任务切换
开销成为一个重要因素。

来自 MSDN

真的,你可以切勿强制激活特定数量的任务。只需创建任务并允许运行时决定哪些任务应该/可以运行。

如果您不喜欢这样,请不要使用 TPL 并带上您自己的线程。

Why do you NEED a certain number of tasks? Let the pool/API decide.

Passing data to tasks is just like passing data to threads: share a data structure.

Use Task to return a result from a task

EDIT

You cannot set the number of worker threads or the number of I/O
completion threads to a number smaller than the number of processors
in the computer.

If the common language runtime is hosted, for example by Internet
Information Services (IIS) or SQL Server, the host can limit or
prevent changes to the thread pool size.

Use caution when changing the maximum number of threads in the thread
pool. While your code might benefit, the changes might have an adverse
effect on code libraries you use.

Setting the thread pool size too large can cause performance problems.
If too many threads are executing at the same time, the task switching
overhead becomes a significant factor.

(from the MSDN)

Really, you can never force a specific number of tasks to be active. Just create the tasks and allow the run-time to decide which ones should/can run.

If you don't like that, do not use the TPL and bring your own threads.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文