Home > Net >  How multi-threaded download files, and the progress bar, like idm
How multi-threaded download files, and the progress bar, like idm

Time:11-08

The single thread using asynchronous I download
Part of the code:
 public void DownloadFile (string urlAddress, string location) 
{
Using (webClient=new webClient ())
{
WebClient. DownloadFileCompleted +=new AsyncCompletedEventHandler (Completed);
WebClient. DownloadProgressChanged +=new DownloadProgressChangedEventHandler (ProgressChanged);

Try
{
Uri URL;
URL=new Uri (urlAddress);
Sw. Start ();
//starts the download
WebClient. DownloadFileAsync (URL, location);
}
The catch (Exception ex)
{
MessageBox. Show (ex. Message);
}
}
}

How to proceed with multi-threaded download

CodePudding user response:

Multithreading is not a very advanced technology, don't move, "multi-threaded"

Since you
//starts the download
WebClient. DownloadFileAsync (URL, location);

So wrote, so have to multithreaded senior??

For (I to 100)
{
DownloadFile (string urlAddress, string location)//do not???????
}

CodePudding user response:

Induction is a multi-threaded implementation, do you want to download file cut into several pieces at the same time? That you study what is breakpoint continuingly, then several Thread to execute the new line,

CodePudding user response:

https://mp.weixin.qq.com/s/MLMjJ7hW-Apr2JHyJ9CgZQ + multithreading, breakpoint continuingly multi-tasking download the example code,
Principle is the request to the server through the Range of the RequestHeader specifies the data fragments,

CodePudding user response:

Do you want a file, multiple threads to download, or multiple files, multiple threads to download?

CodePudding user response:

WnloadFile (string urlAddress, string location)
{
Using (webClient=new webClient ())
{
WebClient. DownloadFileCompleted +=new AsyncCompletedEventHandler (Completed);
WebClient. DownloadProgressChanged +=new DownloadProgressChangedEventHandler (ProgressChanged);

Try
{
Uri URL;
URL=new Uri (urlAddress);
Sw. Start ();
//starts the download
WebClient. DownloadFileAsync (URL, location);

CodePudding user response:

Comments to learn

CodePudding user response:

Encapsulated into entity class and then loop method

CodePudding user response:

references to kill 5 floor matt, bull response:
do you want a file to download multiple threads, or multiple files, multiple threads to download?

A file, multi-threaded download, what method

CodePudding user response:


Program attached pictures

CodePudding user response:

Partitioned download first, server to support the , you can download with multiple threads to accelerate,
General method you can use the HEAD first sends an Http request, to see if the server supports, assuming that the server returns:
.
Accept - Ranges: bytes
Content - Length: 146515
.
Show that the download file length is 146515 bytes, and server support in bytes (bytes) of the block,
At this time, you can use multiple threads to download, each thread request different block, finally, put all the pieces assembled download is complete, such as

The request can be:
GET 832 - Windows. Exe HTTP/1.1
Range: bytes=0-1023
.
The request can be:
GET 832 - Windows. Exe HTTP/1.1
Range: bytes=1024-2047
.
Etc.


Specific 4th floor, you can also refer to X - I - n friends offered links,
X
refer to 4th floor - I - n reply:
https://mp.weixin.qq.com/s/MLMjJ7hW-Apr2JHyJ9CgZQ + multithreading, breakpoint continuingly multi-tasking download the example code,
Principle is the request to the server through the Range of the RequestHeader specifies the data fragments,

CodePudding user response:

Society without a database, without the UI code, at least you can do it in this way can we calculate on the

Thread safety list
Static control signal (5)//slim download resources are limited, the normal situation, you can see, most people will control the number of parallel downloads, parallel number is usually 5

Download the class encapsulation: InotifityProperychanged
{
Object to download progress

Enumeration state: waiting for download, download the complete

Task to download
}

We are too lazy to write specific code, logic implementation difficulty is not great, as for the UI, according to a list, you can bind with InotifityProperychanged can inform you of progress update, began to download method allows you to download (of course what pause, continue this class I will not say what, difficulty)

CodePudding user response:

Want to get them to the total file size,
Then according to the received bytes to judge the download progress

CodePudding user response:

Put the progress bar in the form, according to the actual progress or virtual schedule, simulate the progress bar 0 to 100% effect,
  •  Tags:  
  • C#
  • Related