这个类模拟的是数据的接受和处理using System; using System.Threading; using System.Collections;namespace WindowsApplication1 { //声明代理和参数 public class TransferEventArgs:System.EventArgs { public string message; } public delegate void TransferHandler(TransferEventArgs e); public class Aysn { public event TransferHandler TransferEvent;
private Timer downTimer = null; public Aysn() { // // TODO: 在此处添加构造函数逻辑 // } private void ReceiveCallback(IAsyncResult ar ) { //得到的字节数 int bytesRead = client.EndReceive(ar);
if (bytesRead > 0) { ArrayList al = new ArrayList(); //将得到的数据封装,传入timer al.Add(bytesRead); DownloadTimerStart(al); client.BeginReceive(state.buffer,0,StateObject.BufferSize,0,new AsyncCallback(ReceiveCallback), state);
}
} //Timer的回调 private void DownloadTimerStart(Object stateObject) { //Timer的回调timerCallback绑定的事件为CheckDownloadStatus TimerCallback timerCallback = new TimerCallback(CheckDownloadStatus); //timerCallback是timer的回调,stateObject是封装的数据,0表示立即开始timer,1000是timer的间隔时间为1000ms Timer downloadTimer = new Timer(timerCallback,stateObject,0,1000);
2.需要知道发送或传输的数据量
3.设置timer(推荐用System.Thread中的timer)定时进行数据处理。
4.推荐用在timer定时的处理中用事件抛出
5.在Progressbar中异步处理
用这个Timer来管理使Label显示的那个线程!!
\
using System.Threading;
using System.Collections;namespace WindowsApplication1
{
//声明代理和参数
public class TransferEventArgs:System.EventArgs
{
public string message;
}
public delegate void TransferHandler(TransferEventArgs e); public class Aysn
{
public event TransferHandler TransferEvent;
private Timer downTimer = null; public Aysn()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private void ReceiveCallback(IAsyncResult ar )
{
//得到的字节数
int bytesRead = client.EndReceive(ar);
if (bytesRead > 0)
{
ArrayList al = new ArrayList();
//将得到的数据封装,传入timer
al.Add(bytesRead);
DownloadTimerStart(al); client.BeginReceive(state.buffer,0,StateObject.BufferSize,0,new AsyncCallback(ReceiveCallback), state);
}
}
//Timer的回调
private void DownloadTimerStart(Object stateObject)
{
//Timer的回调timerCallback绑定的事件为CheckDownloadStatus
TimerCallback timerCallback = new TimerCallback(CheckDownloadStatus);
//timerCallback是timer的回调,stateObject是封装的数据,0表示立即开始timer,1000是timer的间隔时间为1000ms
Timer downloadTimer = new Timer(timerCallback,stateObject,0,1000);
this.downTimer = downloadTimer;
#if DEBUG
Console.WriteLine("Download timer started!");
#endif
this.downloadCounter = 0;
this.downTimer = downloadTimer;
} private void CheckDownloadStatus(Object stateObject)
{
//得到数据
ArrayList al = (ArrayList)stateObject;
long fileSize = (long)al[0];
//数据处理
//.......
//....... //发出事件
TransferEventArgs ee = new TransferEventArgs();
ee.message = "Percent";
TransferEvent(ee);
if("数据传输完毕")
{
//timer Dispose
#if DEBUG
Console.WriteLine("Receive Down!Disposing of timer...");
#endif
this.downTimer.Dispose();
this.downTimer = null;
}
} }
} Form1模拟的是UI,只有一个label,form load事件处理初始化.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; //声明处理异步调用的代理
private delegate void DisplayHandler(string message);
private DisplayHandler displayerHandler; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(168, 152);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(496, 373);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void Form1_Load(object sender, System.EventArgs e)
{
//代理绑定labelDiplay事件
displayerHandler = new DisplayHandler(LabelDisplay); Aysn aysn = new Aysn();
//每个数据统计的事件绑定到aysn_TransferEvent
aysn.TransferEvent += new TransferHandler(aysn_TransferEvent);
} private void aysn_TransferEvent(TransferEventArgs e)
{
//在数据统计事件中异步调用显示的代理
this.BeginInvoke(displayerHandler,new object[]{e.message});
} //此方法仅负责显示
private void LabelDisplay(string message)
{
this.label1.Text = message;
}
}
}
int rby=0;
while ( rby<this.FileSize)
{
nReadByte = fs.Read(ByteArray,0,ByteArray.Length) ;
stream.Write( ByteArray,0, nReadByte );
rby+=nReadByte; //我想在此把传输数据rby刷新到Label(labMessage)控件上去
//---------------------------------end pbMessage.Step = nReadByte;
pbMessage.PerformStep();
}