Talk less, first show a
It's a simple process.
In fact, the development process will always meet the project want to apply the download file ~
See other languages there are many well-packaged libraries that can be used ~ ~
As a small white I did not find many examples of C # can be consulted
After finding a demo of "Mutthreaddownloadfile"
But each time the use of the feeling is not convenient, additional information can be expanded is not particularly strong.
It then re-improves and encapsulates the form of the class library, which can be easily used
Post this demo code to see
PublicForm1 () {InitializeComponent (); } DownLoadFile DLF=NewDownLoadFile (); Private voidbtnTest_Click (Objectsender, EventArgs e) { string[] lines = File.ReadAllLines ("huajun software. txt"); for(inti =0; I < lines. Length; i++) { string[] line = Lines[i]. Split (New string[] {"|"}, Stringsplitoptions.removeemptyentries); if(line. Length = =2) { stringPath = uri.escapeuristring (line[1]); stringfilename =Path.getfilename (Path); stringDIR =@"D:\test"; ListViewItem Item= LISTVIEW1.ITEMS.ADD (NewListViewItem (New string[] {(ListView1.Items.Count +1). ToString (), filename,"0","0","0%","0","0", DateTime.Now.ToString (),"Waiting in", line[1] })); intID =item. Index; Dlf. Adddown (Path, dir,"", id); }} DLF. Startdown (); } Private voidForm1_Load (Objectsender, EventArgs e) {DLF. Threadnum=3;//number of threads, not set default to 3Dlf.dosendmsg + = Sendmsghander;//download process handling Events } Private voidSendmsghander (downmsg msg) {Switch(Msg. TAG) { CaseDownstatus.start: This. Invoke ((MethodInvoker)Delegate() {listview1.items[msg. ID]. subitems[8]. Text ="Start Download"; Listview1.items[msg. ID]. subitems[7]. Text =DateTime.Now.ToString (); }); Break; Casedownstatus.getlength: This. Invoke ((MethodInvoker)Delegate() {listview1.items[msg. ID]. subitems[3]. Text =Msg. Lengthinfo; Listview1.items[msg. ID]. subitems[8]. Text ="Connection Successful"; }); Break; CaseDownstatus.end: CaseDownstatus.download: This. Invoke (NewMethodInvoker (() = { This. Invoke ((MethodInvoker)Delegate() {listview1.items[msg. ID]. subitems[2]. Text =Msg. Sizeinfo; Listview1.items[msg. ID]. subitems[4]. Text = Msg. Progress.tostring () +"%"; Listview1.items[msg. ID]. subitems[5]. Text =Msg. Speedinfo; Listview1.items[msg. ID]. subitems[6]. Text =Msg. Surplusinfo; if(Msg. Tag = =downstatus.download) {listview1.items[msg. ID]. subitems[8]. Text ="in the download"; } Else{listview1.items[msg. ID]. subitems[8]. Text ="Download Complete"; } application.doevents (); }); })); Break; CaseDownstatus.error: This. Invoke ((MethodInvoker)Delegate() {listview1.items[msg. ID]. subitems[6]. Text ="failed"; Listview1.items[msg. ID]. subitems[8]. Text =Msg. Errmessage; Application.doevents (); }); Break; } }
Apply without regard to the download process
Just need to add the downloaded file
Set up the download thread and download a few files at a time (default is 3).
I hope this package can help other friends who need to apply to this kind of library.
Click to download
Because of the time of the relationship, just spent two days of testing and improvement, may be considered in many places is not very comprehensive (temporarily did not find too obvious bug)
Everyone can apply and test post feedback in this place, so that we better improve it together!
C # Multithreading multi-file bulk Download