如何在c#中运行并发任务

时间:2015-12-16 10:18:43

标签: c# task

我编写了一个控制台应用程序来处理图像文件并将它们与数据库记录相匹配。

图像文件全部位于不同文件夹的文件存储中,每天一个文件夹,因此文件存储的结构将是

Get-ChildItem -Path .. -Recurse

所以我的程序将打开每个文件夹抓取索引文件并将图像匹配到数据库中的记录。

文件夹非常大,其中一些有90000+图像,所以我想运行最多5个不同的任务,每个抓取一个不同的文件夹并处理它们,我尝试创建5个任务,并且工作正常,除了一旦完成了5项任务中的一项,我不知道如何开始新任务。

任何指针都会受到赞赏。

感谢。

2 个答案:

答案 0 :(得分:1)

简单的方法是创建包含所有文件的列表并使用Parallel.ForEach,结帐文档https://msdn.microsoft.com/en-us/library/dd460720%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

示例代码:

// A simple source for demonstration purposes. Modify this path as necessary.
String[] files = System.IO.Directory.GetFiles(@"C:\Users\Public\Pictures\Sample Pictures", "*.jpg");
String newDir = @"C:\Users\Public\Pictures\Sample Pictures\Modified";
System.IO.Directory.CreateDirectory(newDir);

// Method signature: Parallel.ForEach(IEnumerable<TSource> source, Action<TSource> body)
// Be sure to add a reference to System.Drawing.dll.
Parallel.ForEach(files, (currentFile) => 
    {
        // The more computational work you do here, the greater 
        // the speedup compared to a sequential foreach loop.
        String filename = System.IO.Path.GetFileName(currentFile);
        var bitmap = new Bitmap(currentFile);

        bitmap.RotateFlip(RotateFlipType.Rotate180FlipNone);
        bitmap.Save(Path.Combine(newDir, filename));

        // Peek behind the scenes to see how work is parallelized.
        // But be aware: Thread contention for the Console slows down parallel loops!!!

        Console.WriteLine("Processing {0} on thread {1}", filename, Thread.CurrentThread.ManagedThreadId);
        //close lambda expression and method invocation
    });


// Keep the console window open in debug mode.
Console.WriteLine("Processing complete. Press any key to exit.");
Console.ReadKey();

答案 1 :(得分:1)

您应该在任务列表中使用WhenAny(非阻止)或WaitAny(阻止)。然后从列表中删除任何已完成的任务并添加新任务。

List<Task<...>> tasks = ... // put your 5 tasks inside this list.

while(condintion)
{
    await Task.WhenAny(tasks);
    tasks.RemoveAll(t => t.IsCompleted);
    while(tasks.Count < 5 && condintion)
    {
        Task<...> newTask = ... // run your task
        tasks.Add(newTask);
    }
}

设置适当的条件,以便在获取所有文件夹时仅为false。