C # Multithreading multi-file bulk Download

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.