我应该在 UI 线程或 Task.Run 上使用异步等待来处理多个文件吗? [英] Should I use async await on UI Thread or Task.Run to process multiple files?

查看:19
本文介绍了我应该在 UI 线程或 Task.Run 上使用异步等待来处理多个文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 .NET 4.5 在 WPF 中编写应用程序.该应用程序允许用户选择多个文件并导入它们.发生这种情况时,应用程序将解析文本文件并将数据存储到数据库中.这些文件可能非常大,所以我希望 UI 保持响应并允许用户在发生这种情况时做其他事情.

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.

我是异步编程的新手,想知道最好的方法是什么?根据微软文章...

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

"async 和 await 关键字不会导致创建额外的线程.异步方法不需要多线程,因为异步方法不在自己的线程上运行.该方法在当前同步上下文中运行并使用时间仅当方法处于活动状态时才在线程上运行.您可以使用 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",因为它们将在单独的线程上运行?如果没有,使用 async/await 在同一线程上运行所有内容有什么好处?

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 的性质,按顺序访问它们会更快.不过,这将取决于磁盘类型(例如,SSD 与常规"磁盘的行为不同).

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).

这篇关于我应该在 UI 线程或 Task.Run 上使用异步等待来处理多个文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆