我应该使用异步的UI线程或Task.Run等待处理多个文件?

我应该使用异步的UI线程或Task.Run等待处理多个文件?

问题描述:

我写使用.NET 4.5中WPF应用程序。该应用程序允许用户选择多个文件,并将其导入。当发生这种情况,应用程序将解析的文本文件并将该数据存储到数据库中。所以我想在用户界面保持响应,并允许用户做其他事情,而发生这种情况的文件可能会非常大。

I am writing an application in WPF using .NET 4.5. The application allows the user to select multiple files and import them. When this happens the application will parse text files and store the data into a database. The files can be very large so I want the UI to remain responsive and allow the user to do other things while this is happening.

我是新来的异步编程,并会知道最好的办法是什么?根据 Microsoft文章 ...

I'm new to asynchronous programming and would to know what the best approach would be? According to a Microsoft article...

的异步和等待关键字不会导致创建额外的线程。异步方法不需要多线程,因为一个异步方法不会在自己的线程中运行。该方法对当前同步上下文运行,并使用时间只有当方法是活跃的线程。你可以使用Task.Run移动CPU绑定的工作提高到一个后台线程,而是后台线程不与这只是等待结果变得可用一个过程中提供帮助。

"The async and await keywords don't cause additional threads to be created. Async methods don't require multithreading because an async method doesn't run on its own thread. The method runs on the current synchronization context and uses time on the thread only when the method is active. You can use Task.Run to move CPU-bound work to a background thread, but a background thread doesn't help with a process that's just waiting for results to become available."

由于我处理多个文件,我会用Task.Run为每个文件,因为它们将在单独的线程运行的更好?如果没有,什么是使用异步同一个线程上运行的一切优势/等待?

Since I am processing multiple files would I be better of using "Task.Run" for each file since they will run on separate threads? If not, what is the advantage of running everything on the same thread using async/await?

请注意,没有其他的UI操作(除进度条 - 进度报告)真正关心当这些文件被做处理所以根据这篇文章好像使用Task.Run将有利于我。你怎么看?

Note that no other UI operation (except progress bars - progress reporting) really cares about when these files are done processing so according to this article it seems like using Task.Run would benefit me. What are your thoughts?

什么是你的文件做什么?如果你正在做什么CPU密集型,倒不如以移动处理过的UI线程。如果它真的只是将是IO,那么你就可以做到这一切在UI线程 - 和还是的使其平行

What are you doing with the files? If you're doing anything CPU intensive, it would be better to move that processing off the UI thread. If it's really just going to be the IO, then you can do it all in the UI thread - and still make it parallel.

例如:

private async Task ProcessAllFiles(IEnumerable<string> files)
{
    List<Task> tasks = files.Select(x => ProcessFile(x))
                            .ToList();
    await Task.WhenAll(tasks);
    textBox.Text = "Finished!";
}

private async Task ProcessFile(string file)
{
    // Do stuff here with async IO, and update the UI if you want to...
}

在这里,你仍然在做UI线程上的所有实际处理 - 没有单独的线程在运行,超出潜在IO完成端口 - 但你仍然在同一时间启动多个异步IO操作

Here you're still doing all the actual processing on the UI thread - no separate threads are running, beyond potentially IO completion ports - but you're still starting multiple asynchronous IO operations at the same time.

现在另外一个要考虑的就是这个的可能的居然慢下来的一切反正。如果你在同一个物理磁盘上处理多个文件,你可能会发现,他们的访问顺序会更快只是由于磁盘IO的性质。这将取决于磁盘类型,但(固态硬盘将采取行动不同,以正规军的磁盘,例如)。

Now one other thing to consider is that this might actually slow everything down anyway. If you're processing multiple files on the same physical disk, you might find that accessing them sequentially would be faster just due to the nature of disk IO. It will depend on the disk type though (SSDs would act differently to "regular" disks, for example).