看看一下思路对你是否有帮助
定义一个参数类
public class ReEventArgs:EventArgs
{
private int totalTime=10000;//客户自己设定的时间长度
public int Total
{
get {return totalTime;}set {totalTime=value;}
}
private long recieveBytes
public long ReiecveBytes //接收到的字节数
{
get {return recieveBytes;}set {recieveBytes=value;}
}
}class yourClass
{
private delegate void progress(object sender,ReEventArgs e);
public event progress OnProgress;//定义事件,每接收一定的字节数将引发此事件public void Recieve()
{
你的异步调用的代码
.
.
.假设回调函数是ReciveProgress(IAsyncResult ar);
}
public void ReciveProgress(IAsyncResult ar)
{
...
if (OnProgress!=null)
{
ReEventArgs e=new ReEventArgs();
e.RecieveByte=?//你的代码中应该累计计算接收到的字节数
OnProgress(this,e);
}
...
}
}
在事件OnProgress里加入你所需要的代码就ok啦,比如,设置进度条的总长度和目前进度条的位置
定义一个参数类
public class ReEventArgs:EventArgs
{
private int totalTime=10000;//客户自己设定的时间长度
public int Total
{
get {return totalTime;}set {totalTime=value;}
}
private long recieveBytes
public long ReiecveBytes //接收到的字节数
{
get {return recieveBytes;}set {recieveBytes=value;}
}
}class yourClass
{
private delegate void progress(object sender,ReEventArgs e);
public event progress OnProgress;//定义事件,每接收一定的字节数将引发此事件public void Recieve()
{
你的异步调用的代码
.
.
.假设回调函数是ReciveProgress(IAsyncResult ar);
}
public void ReciveProgress(IAsyncResult ar)
{
...
if (OnProgress!=null)
{
ReEventArgs e=new ReEventArgs();
e.RecieveByte=?//你的代码中应该累计计算接收到的字节数
OnProgress(this,e);
}
...
}
}
在事件OnProgress里加入你所需要的代码就ok啦,比如,设置进度条的总长度和目前进度条的位置
很感谢你的帮助,你的思路我正在研究,看是否可以在我现在的程序中应用.
尤其构造EventArgs的子类来传递参数我没有用过,我会好好研究研究.
现在有个问题就是你关于异步调用的代码没有列出来
我个人认为c#中的那种指定回调函数的异步调用方法很不爽,其实想当于把
同步调用的一个函数劈为两半了,不知你对这方面有什么看法
using System;
using System.IO;
using System.Net;
using System.Windows.Forms;
using System.ComponentModel;
using System.Threading;namespace DownLoadControl
{
public class DownLoadEventArgs : EventArgs
{
private long bytesRecieved;
public long BytesRecieved //接收字节数
{
set {bytesRecieved=value;}
get {return bytesRecieved;}
} private long totalBytes;
public long TotalBytes //总字节数
{
set {totalBytes=value;}
get {return totalBytes;}
} }
class DownLoadInfo
{
private long bytesRead=0;
public long BytesRead
{
set {bytesRead=value;}
get {return bytesRead;}
}
private long bytesTotal=-1;
public long BytesTotal
{
set {bytesTotal=value;}
get {return bytesTotal;}
}
public byte[] bytesBuffer=new byte[1024];
private WebRequest request=null;
public WebRequest Request
{
set {request=value;}
get {return request;}
}
private Stream responseStream=null;
public Stream ResponseStream
{
set {responseStream=value;}
get {return responseStream;}
} }
public class DownLoadControl:Component
{
public delegate void Complete(object sender,DownLoadEventArgs e);
public delegate void Progress(object sender,DownLoadEventArgs e); public event Complete OnComplete; //下载完成事件
public event Progress OnProgress; //下载进度事件 private DownLoadInfo downLoadInfo=null;
private FileStream downLoadStream=null;
private ManualResetEvent allDone=new ManualResetEvent(false);
public DownLoadControl()
{
downLoadInfo=new DownLoadInfo();
}
protected void CompleteDownLoad(IAsyncResult ar)
{
WebResponse response=downLoadInfo.Request.EndGetResponse(ar);
downLoadInfo.ResponseStream=response.GetResponseStream();
downLoadInfo.BytesTotal=Convert.ToInt64(response.Headers["Content-Length"]);//获取需要下载的总字节数
downLoadInfo.ResponseStream.BeginRead(downLoadInfo.bytesBuffer,0,1024,new AsyncCallback(DownLoadProgress),downLoadInfo);
}
protected void DownLoadProgress(IAsyncResult ar)
{
int dataRead=downLoadInfo.ResponseStream.EndRead(ar);
if (dataRead>0)
{
downLoadStream.Write(downLoadInfo.bytesBuffer,0,downLoadInfo.bytesBuffer.Length);
downLoadInfo.BytesRead+=dataRead;
if (this.OnProgress!=null)
{
DownLoadEventArgs args=new DownLoadEventArgs();
args.TotalBytes=downLoadInfo.BytesTotal;
args.BytesRecieved=downLoadInfo.BytesRead;
this.OnProgress(this,args);
}
downLoadInfo.ResponseStream.BeginRead(downLoadInfo.bytesBuffer,0,1024,new AsyncCallback(DownLoadProgress),downLoadInfo);
}
else
{
downLoadInfo.ResponseStream.Close();
downLoadStream.Close();
allDone.Set();
}
}
protected void DownLoad()
{
try
{
downLoadInfo.Request=WebRequest.Create(new Uri(this.URL)); //建立连接
}
catch (Exception E)
{
MessageBox.Show(E.Message);
return;
}
catch
{
MessageBox.Show("无法建立连接");
return;
}
if (File.Exists(this.Distination))
{
if (MessageBox.Show("","文件"+this.Distination+"已经存在,是否代替原文件?",MessageBoxButtons.YesNo)==DialogResult.No)
return;
}
try
{
downLoadStream=File.Create(this.Distination);
}
catch (Exception E)
{
MessageBox.Show(E.Message);
return;
}
catch
{
MessageBox.Show("无法建立指定的文件");
return;
}
allDone.Reset();
downLoadInfo.Request.BeginGetResponse(new AsyncCallback(CompleteDownLoad),downLoadInfo); //开始接收数据
allDone.WaitOne();
if (this.OnComplete!=null)
{
DownLoadEventArgs args=new DownLoadEventArgs();
args.TotalBytes=downLoadInfo.BytesTotal;
args.BytesRecieved=downLoadInfo.BytesRead;
this.OnProgress(this,args);
} } public void Start()
{
System.Threading.Thread a=new System.Threading.Thread(new System.Threading.ThreadStart(this.DownLoad));
a.Start();
}
#region 属性
private string url;
public string URL
{
set {url=value;}
get {return url;}
}
private string distination;
public string Distination
{
set {distination=value;}
get {return distination;}
}
public long BytesRead
{
get {return downLoadInfo.BytesRead;}
}
public long BytesTotal
{
get {return downLoadInfo.BytesTotal;}
}
#endregion
}
}