如何使用具有这些属性的 C# 4.0 任务功能 - 在运行时分配变量 - 保持一定数量的任务处于活动状态
好吧,我已经搜索了好几天了,但没有任何例子说明我想要实现的目标。目前我可以使用任务进行多线程爬行,但写得非常糟糕。您可以从这里看到完整的代码:
现在是我的问题。我想使用 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 :
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否已研究过 TPL-DataFlow CPL:
这是测试版,但我认为你的问题不是线程。
似乎您正在尝试构建某种基于代理的编程模型,并且此 DataFlow 为您提供了一些非常好的工具。
这是主页,这是一个不错的视频。
Have you looked into the TPL-DataFlow CPL yet:
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.
为什么需要一定数量的任务?让池/API 决定。
将数据传递给任务就像将数据传递给线程一样:共享数据结构。
使用任务返回任务结果
编辑
(来自 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
(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.