C#,Asp.Net多线程断点续传下载
C#,Asp.Net多线程断点续传下载
程序截图: 主窗体源代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Diagnostics; using System.IO; namespace BreakDownload { public partial class frmBreakDownload : Form { private DataTable _dtFileInfos = null; private DataSet _dsFileInfo = null; private DownloadThread _CurrDownload = null; private const string FILE_NAME = "Download.Log"; public frmBreakDownload() { InitializeComponent(); } private void frmBreakDownload_Load(object sender, EventArgs e) { if (!File.Exists(FILE_NAME)) { _dsFileInfo=new DataSet(); this.CreateTable(); } } private void btnStop_Click(object sender, EventArgs e) { this.StopTask(); } private void btnStart_Click(object sender, EventArgs e) { this.StartTask(); } private void btnRestart_Click(object sender, EventArgs e) { this.RestartTask(); } public void StartTask() { //Create new row to save download file info DataRow dr = _dtFileInfos.NewRow(); _dtFileInfos.Rows.Add(dr); //Create object _CurrDownload = new DownloadThread( txtURL.Text, "", @"c:\" + DateTime.Now.ToString("yyyyMMddHHmmss"), dr, 1); _CurrDownload.Finished += new ThreadFinishedHandler(Finished); _CurrDownload.DataReceived = new DataReceivedEventHandler(DataReceived); _CurrDownload.Start(); } public void RestartTask() { // Read thread info DataSet dsThreadInfo = new DataSet(); foreach (DataRow dr in _dtFileInfos.Rows) { dsThreadInfo.ReadXml(dr["CreatedTime"].ToString() + ".xml"); } if (dsThreadInfo.Tables.Count <= 0) return; // Init download object // Restart to download _CurrDownload = new DownloadThread(_dtFileInfos.Rows[0], dsThreadInfo.Tables[0]); _CurrDownload.Finished += new ThreadFinishedHandler(Finished); _CurrDownload.DataReceived = new DataReceivedEventHandler(DataReceived); _CurrDownload.Start(); btnStop.Enabled = true; } public void StopTask() { // Set stop message to all _CurrDownload.Stop(); // Save log file _dtFileInfos.AcceptChanges(); _dsFileInfo.WriteXml(FILE_NAME); foreach (DataRow dr in _dtFileInfos.Rows) { DataSet dsThreadInfo = null; DataTable dt = _CurrDownload.ThreadInfo; if (dt.DataSet == null) { dsThreadInfo = new DataSet(); dsThreadInfo.Tables.Add(_CurrDownload.ThreadInfo); } else dsThreadInfo = dt.DataSet; dsThreadInfo.WriteXml(dr["CreatedTime"].ToString() + ".xml"); } } private void LoadFile() { _dsFileInfo = new DataSet(); if (!File.Exists(FILE_NAME)) { CreateTable(); return; } _dsFileInfo.ReadXml(FILE_NAME); if (_dsFileInfo.Tables.Count == 0) this.CreateTable(); else _dtFileInfos = _dsFileInfo.Tables["DownloadFileInfo"]; } private void CreateTable() { _dtFileInfos = new DataTable("DownloadFileInfo"); _dtFileInfos.Columns.Add("RequestURL", typeof(string)); _dtFileInfos.Columns.Add("ResponseURL", typeof(string)); _dtFileInfos.Columns.Add("FileName", typeof(string)); _dtFileInfos.Columns.Add("TotalSize", typeof(int)); _dtFileInfos.Columns.Add("BlockLeftSize", typeof(int)); _dtFileInfos.Columns.Add("CreatedTime", typeof(string)); _dsFileInfo.Tables.Add(_dtFileInfos); } private void DataReceived(DownLoadEventArgs e) { _CurrDownload.UpdateDownloadInfo(e.ThreadNO, e.StartPosition, e.ReadCount); Debug.WriteLine(string.Format("Thread NO:{0} read {2} bytes from {1} postion!", e.ThreadNO, e.StartPosition, e.ReadCount)); } private void Finished() { _dtFileInfos.AcceptChanges(); _dsFileInfo.WriteXml(FILE_NAME); //your log file btnStop.Enabled = false; btnRestart.Enabled = false; btnDownload.Enabled = true; MessageBox.Show("Download finished"); } } } 跟踪记录 开始下载... Thread NO:0 begins to download! Thread NO:0 call function named DownloadData! Thread NO:0 reads data! Thread NO:0 writes data! Thread NO:0 writes 3768 data! Thread NO:0 send callback info! Thread NO:0 read 3768 bytes from 0 postion! Thread NO:0 reads data! Thread NO:0 writes data! Thread NO:0 writes 65536 data! Thread NO:0 send callback info! 暂停下载... The thread 0x13e0 has exited with code 0 (0x0). Thread NO:0 is stopped! The thread 0xefc has exited with code 0 (0x0). 暂停后重新下载... Thread NO:0 begins to download! Thread NO:0 call function named DownloadData! Thread NO:0 reads data! Thread NO:0 writes data! Thread NO:0 writes 3763 data! Thread NO:0 send callback info! Library.cs 文件实现了多线程断点续传, 本演示程序仅支持启动一个下载任务. 参考Library.cs的原创文章:http://blog.csdn.net/Knight94/archive/2006/08/04/1018305.aspx
参考文档:
C#多线程处理多个队列的数据(交叉线程访问及Invoke方法使用) C# ASP.NET 实现文件断点续传 C#异步操作等待窗体,异步多线程处理数据通用界面(frmThreadOperating) C#多线程异步处理数据通用界面窗体(frmThreadOperating) C#源码-淘特旅游网站管理系统Asp.Net版 4.3-源码下载 C#源码-OA网络办公自动化系统asp.NET-源码下载 C#源码-大型综合管理ERP源码 大型ERP源码 bs框架 asp.net源码 界面美观-源码下载 C#源码-ASP.NET 4.0电子商城源码-源码下载 C#源码-Asp.net明日文库源码-源码下载 C#源码-[博客空间]X3BLOG(ASP.NET开源多用户博客系统) 1.1.0 beta1 源代码_x3blog-源码下载 C#源码-[电子商务]NopCommerce电子商城系统 v1.20_nopcommercepromotion-源码下载 C#源码-[电子商务]YxShop易想购物商城系统 v4.3.1 bulid 090515 源码版_changehope_b2c_4-源码下载 C#源码-[论坛社区]BBSGood论坛程序 ASP.NET版-源码下载 [论坛社区]Asp.Net Forums v2.3.2130 官方中文版-源码下载 [人才房产]青岛房产网 2008 (C#.net+Sql server)-源码下载
其它资料:
什么是C/S结构? | C/S框架核心组成部分 | C/S框架-WebService部署图 | C/S框架-权限管理 | C/S结构系统框架 - 5.1旗舰版介绍 | C/S结构系统框架 - 功能介绍 | C/S结构系统框架 - 产品列表 | C/S结构系统框架 - 应用展示(图) | 三层体系架构详解 | C/S架构轻量级快速开发框架 | C/S框架网客户案例 | WebApi快速开发框架 | C/S框架代码生成器 | 用户授权注册软件系统 | 版本自动升级软件 | 数据库底层应用框架 | CSFramework.CMS内容管理系统 | |