使用TPL文件下载调度和处理每个这些文件 [英] Using TPL for file download scheduler and processing each of those files
问题描述
我目前的项目使用.NET 2.0线程,拥有一个主线程调度1.FILE下载线程和2文件处理线程。我可以限制在平行于被处理的最大线程,说16(可能是10个文件的下载和6文件处理已经下载的文件)。我想使用TPL迁移我的code。
My current project is using .net 2.0 threading and has a master thread for scheduling 1.File download thread and 2. File Processing Thread. I can restrict the maximum threads to be processed in parallel, say 16 (May be 10 file download and 6 file processing for the already downloaded files). I would like to migrate my code using TPL.
Thread workerThread = null;
switch (Status)
{
case StatusEnum.FileWatchLocked:
workerThread = new Thread(workflowMgr.GetFiles);
break;
case FPFStatusEnum.ProcessLocked:
workerThread = new Thread(workflowMgr.ProcessFiles);
break;
}
lock (_threadCountMonitor)
{
_workFlowPool.Add(workerThread, workflowMgr);
_workFlowThreadIDPool.Add(workerThread.ManagedThreadId, workerThread);
workerThread.Start();
++_threadCount;
}
我需要跟踪,如果任务完成,我可以排队了一些更多的工作。此外,我需要实现TPL的GETFILE和processfile。什么是从第三方物流的最佳方法?
I need to track if a task is completed and I can queue some more work. Also I need to implement TPL for getfile and processfile. What would be the best approach from TPL?
推荐答案
我觉得你的code可以这样写如下。这样,您就不需要手动排队的工作项目。
I think your code can be written like below. In this way you don't need to manually queue the work items.
foreach (var uri in uris)
{
string filename = "urixxx"; //get local file name
Task.Factory.StartNew(() =>
{
DownloadFile(uri, filename);
}).ContinueWith((t) =>
{
ProcessFile(filename);
});
}
这篇关于使用TPL文件下载调度和处理每个这些文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!