Using TPL for file download scheduler and processi

2019-07-25 16:28发布

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;
}

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?

1条回答
爷的心禁止访问
2楼-- · 2019-07-25 16:46

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);
            });
}
查看更多
登录 后发表回答