[英]Multi-threaded downloader in C#
I'm working on a WPF download manager which should support downloading multiple files at the same time through HTTP, adding a new download, pause/resume, displaying and updating each download process information (filename, size, download percentage, time left...) in a DataGrid, displaying progress in a ProgressBar and a bunch of other stuff. 我正在使用WPF下载管理器,该管理器应支持通过HTTP同时下载多个文件,添加新的下载,暂停/继续,显示和更新每个下载过程信息(文件名,大小,下载百分比,剩余时间。)。 。)在DataGrid中显示进度条中的进度以及其他内容。
So, the idea is to use a separate thread for each download process, and be able to dynamically create/cancel threads. 因此,其想法是为每个下载过程使用一个单独的线程,并能够动态创建/取消线程。 What is the best way to accomplish this? 做到这一点的最佳方法是什么? Using a BackgroundWorker, ThreadPool? 使用BackgroundWorker,ThreadPool吗?
The WebClient XXXAsync methods such as DownloadFileAsync
already provide you with asynchronous versions of all methods that allow you to perform the HTTP requests on separate threads. WebClient XXXAsync方法(例如DownloadFileAsync
已经为您提供了所有方法的异步版本,这些版本允许您在单独的线程上执行HTTP请求。 They are also C# 5.0 async ready. 它们也已为C#5.0异步就绪。
Since this is a WPF application you should ensure that you are modifying the UI controls only on the main UI thread using the Dispatcher.BeginInvoke method. 由于这是WPF应用程序,因此应确保使用Dispatcher.BeginInvoke方法仅在主UI线程上修改UI控件。
我只是直接使用Thread
,并保留对我创建的所有踏步的引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.