Implementation principle: The use of WebClient for bulk download tasks, simple simulation thunder download effect!
Don't say much nonsense, first look at the disguise effect:
The specific implementation steps are as follows:
1. New Project: Winbatchdownload
2. First build a Windows Form: frmbatchdownload, Load event frmbatchdownload_load
3. Place a button: Btnstartdownload, single event Btnstartdownload_click
4. Place a datagridview:dgvdownload.
5. The specific code is as follows:
Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing; Using system.text;using System.Windows.Forms; #region namespace using system.threading;using System.runtime.interopservices;using system.net;using System.Collections, #endregionnamespace winbatchdownload{ Public partial class Frmbatchdownload:form {#region Global member//store download list list<synfileinfo> M_syn Fileinfolist; #endregion #region Constructor Public frmbatchdownload () {InitializeComponent (); M_synfileinfolist = new list<synfileinfo> (); #endregion #region Form Load event private void Frmbatchdownload_load (object sender, EventArgs e) { Initializes the DataGridView related attribute Initdatagridview (dgvdownload); Add DataGridView Related column information addgridviewcolumns (dgvdownload); New Task Addbatchdownload (); } #endregion #region Add grIdview columns///<summary>///Synchronizing list///</summary> void Addgridviewcolumns (Datagridvi EW DGV) {DGV. Columns.Add (New Datagridviewtextboxcolumn () {datapropertyname = "DocID", Headert ext = "File id", Visible = false, Name = "DocID"}); DGV. Columns.Add (New Datagridviewtextboxcolumn () {AutoSizeMode = Datagridviewautosizecolumnmode.none , DataPropertyName = "DocName", HeaderText = "file name", name = "DocName", Width = 300}); DGV. Columns.Add (New Datagridviewtextboxcolumn () {datapropertyname = "FileSize", Head Ertext = "Size", Name = "FileSize",}); DGV. Columns.Add (New Datagridviewtextboxcolumn () {datapropertyname = "synspeed", Head ERtext = "Speed", Name = "Synspeed"}); DGV. Columns.Add (New Datagridviewtextboxcolumn () {datapropertyname = "synprogress", H Eadertext = "Progress", Name = "synprogress"}); DGV. Columns.Add (New Datagridviewtextboxcolumn () {datapropertyname = "Downpath", Head Ertext = "", Visible = false, Name = "Downpath"}); DGV. Columns.Add (New Datagridviewtextboxcolumn () {datapropertyname = "Savepath", Head Ertext = "Save Address", Visible = false, Name = "Savepath"}); DGV. Columns.Add (New Datagridviewtextboxcolumn () {datapropertyname = "Async", Headert ext = "Async", Visible = false, Name = "Async"}); } #endregion #region Add download task and display to list void Addbatchdownload () {//Empty row data dgvDownLoad.Rows.Clear (); Add a list (build multiple tasks) list<arraylist> arraylistlist = new list<arraylist> (); Arraylistlist.add (New ArrayList () {"0",//File ID "PPTV client. exe",//File name "21.2 MB", File Size "0 kb/s",//download Speed "0%",//Download Progress "http://download.pplive.com/pptvsetup_3.2. 1.0076.exe ",//Remote server" D:\\PPTV client. exe ",//local Save Address true//whether asynchronous}); Arraylistlist.add (New ArrayList () {"1", "PPS client. exe", "14.3 MB", "0 kb/s", "0%", "Http://download.ppstream.com/ppstreamsetup.exe", "D:\\pps Client. exe ", true}); Arraylistlist.add (New ArrayList () {"2", "Beautiful view client. exe", "4.1 MB", "0 kb/s", "0%", "Http://kankan.dl.meitu.com/V2/1029/KanKan_kk360Setup.exe", "D:\\ to see client. exe", true}); foreach (ArrayList ArrayList in arraylistlist) {int rowIndex = DGVDOWNLOAD.ROWS.ADD (arraylist.t Oarray ()); ARRAYLIST[2] = 0; Arraylist.add (Dgvdownload.rows[rowindex]); Remove the row information from the list in the Save list Collection (M_synfileinfolist) M_synfileinfolist.add (New Synfileinfo (Arraylist.toarray ())); }} #endregion #region start the download button standalone event private void Btnstartdownload_click (object sender, Event Args e) {//Determine if the network connection is normal if (isconnected ()) {//settings are not available btnstartdownload.enabled = false; Sets the maximum number of active threads and the number of threads to wait Threadpool.setmaxthreads (3, 3); Determine if there is a task if (m_synfileinfolist.count <= 0) addbaTchdownload (); foreach (Synfileinfo m_synfileinfo in m_synfileinfolist) {//Start download Task S Tartdownload (M_synfileinfo); }} else {MessageBox.Show ("network exception!"); }} #endregion #region Check network status//Detect network Status [DllImport ("Wininet.dll")] extern static BOOL InternetGetConnectedState (out int connectiondescription, int reservedvalue); <summary>///Detect network Status///</summary> bool IsConnected () {int I = 0; BOOL state = InternetGetConnectedState (out I, 0); return state; #endregion #region Download the file using WebClient//<summary>//HTTP to download the remote file and save the local function///< ;/summary> void Startdownload (object o) {synfileinfo m_synfileinfo = (synfileinfo) o; M_synfileinfo.lasttime = DateTime.Now; Again new to avoid WebClient cannot I/o concurrency WebClient client = new WebClient (); if (M_synfileinfo.async) {//asynchronously downloads the client. DownloadProgressChanged + = new Downloadprogresschangedeventhandler (client_downloadprogresschanged); Client. downloadfilecompleted + = new Asynccompletedeventhandler (client_downloadfilecompleted); Client. DownloadFileAsync (New Uri (M_synfileinfo.downpath), M_synfileinfo.savepath, M_synfileinfo); } else client. DownloadFile (New Uri (M_synfileinfo.downpath), M_synfileinfo.savepath); }///<summary>///download progress bar///</summary>//<param name= "Sender" ></param >//<param name= "E" ></param> void Client_downloadprogresschanged (object sender, Downloadpro Gresschangedeventargs e) {synfileinfo m_synfileinfo = (synfileinfo) e.userstate; M_synfileinfo.synprogress= e.progresspercentage + "%"; Double Secondcount = (datetime.now-m_synfileinfo.lasttime). TotalSeconds; M_synfileinfo.synspeed = fileoperate.getautosizestring (convert.todouble (E.bytesreceived/secondcount), 2) + "/S"; The corresponding data in Update DataGridView shows the download Progress m_synfileinfo.rowobject.cells["Synprogress". Value = m_synfileinfo.synprogress; The corresponding data in Update DataGridView shows the download speed (average speed of total progress) m_synfileinfo.rowobject.cells["Synspeed". Value = M_synfileinfo.synspeed; }///<summary>///Download complete call///</summary>//<param name= "Sender" ></para m>//<param name= "E" ></param> void client_downloadfilecompleted (object sender, Asynccomplet Edeventargs e) {//to which a file has been downloaded synfileinfo m_synfileinfo = (synfileinfo) e.userstate; M_synfileinfolist.remove (M_synfileinfo); if (m_synfileinfolist.count <= 0) {//At this time All files are downloaded btnstartdownload.enabled = true; }} #endregion #region need to download the file entity classes class Synfileinfo {public string DocID {g Et Set public string DocName {get; set;} Public long FileSize {get; set;} public string Synspeed {get; set;} public string Synprogress {get; set;} public string Downpath {get; set;} public string Savepath {get; set;} Public DataGridViewRow Rowobject {get; set;} public bool Async {get; set;} Public DateTime lasttime {get; set;} Public Synfileinfo (object[] objectarr) {int i = 0; DocID = Objectarr[i]. ToString (); i++; DocName = Objectarr[i]. ToString (); i++; FileSize = Convert.toint64 (Objectarr[i]); i++; Synspeed = Objectarr[i]. ToString (); i++; synprogress = Objectarr[i].ToString (); i++; Downpath = Objectarr[i]. ToString (); i++; Savepath = Objectarr[i]. ToString (); i++; Async = Convert.toboolean (Objectarr[i]); i++; Rowobject = (DataGridViewRow) objectarr[i]; }} #endregion #region initialization of GridView void Initdatagridview (DataGridView dgv) { DGV. AutoGenerateColumns = false;//Whether the column DGV is created automatically. Allowusertoaddrows = false;//Whether to allow rows to be added (default: TRUE) DGV. Allowusertodeleterows = false;//Whether to allow deletion of rows (default: TRUE) DGV. Allowusertoresizecolumns = false;//whether to allow resizing (default: TRUE) DGV. Allowusertoresizerows = false;//Whether to allow row size adjustment (default: TRUE) DGV. autoSizeColumnsMode = datagridviewautosizecolumnsmode.fill;//column width mode (current fill) (Default: Datagridviewautosizecolumnsmode.none) DGV. BackgroundColor = system.drawing.color.white;//background color (default: ControlDark) DGV. BorderStyle = borderstyle.fixed3d;//Border Style (default: BorderStyle.FixedSingle) DGV. CellbordErstyle = datagridviewcellborderstyle.singlehorizontal;//cell border Style (default: Datagridviewcellborderstyle.single) DGV. Columnheadersborderstyle = datagridviewheaderborderstyle.none;//list Header style (default: Datagridviewheaderborderstyle.single) DGV. Columnheadersheightsizemode = datagridviewcolumnheadersheightsizemode.disableresizing;//whether to allow column size adjustment ( Default: Datagridviewcolumnheadersheightsizemode.enableresizing) DGV. Columnheadersheight = 30;//List Header height (default:) DGV. MultiSelect = false;//supports multiple selection (default: TRUE) DGV. ReadOnly = true;//is read-only (default: false) DGV. Rowheadersvisible = false;//If the wardrobe is displayed (default: TRUE) DGV. SelectionMode = datagridviewselectionmode.fullrowselect;//Select mode (default: Datagridviewselectionmode.cellselect)} #e Ndregion #region File related Operations classes///<summary>////Documents Related Operations class///</summary> Public Class Fileoperate {#region The corresponding unit conversion constant Private Const double kbcount = 1024; Private CoNST Double mbcount = kbcount * 1024; Private Const DOUBLE Gbcount = Mbcount * 1024; Private Const DOUBLE Tbcount = Gbcount * 1024; #endregion #region Get Fit Size//<summary>//Get fit size///</summary> <param name= "Size" > Byte size </param>//<param name= "Roundcount" > Reserved decimals (bit) </param >//<returns></returns> public static string getautosizestring (double size, int rou Ndcount) {if (Kbcount > Size) return math.round (size, Roundcount) + "B"; else if (Mbcount > Size) return Math.Round (Size/kbcount, Roundcount) + "KB"; else if (Gbcount > Size) return Math.Round (Size/mbcount, Roundcount) + "MB"; else if (Tbcount > Size) return Math.Round (Size/gbcount, Roundcount) + "GB"; else return Math.Round (Size/tbcount, Roundcount) + "TB"; } #endregion} #endregion}}
Source: Https://github.com/marblemm/WinBatchDownload
C # Asynchronous bulk download file