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
程序截图:
主窗体源代码:
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
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网