你可以把传输的数据放在一个全局变量上面啊,然后把label放在一个timer1_tick中 ,定时刷新啊!!

解决方案 »

  1.   

    数据我已经知道传了多少了,但就是不能刷新Label控件,Timer_tick好像不起作用!
      

  2.   

    stephenZL(我爱C#,HOHO~~~),我查过Msdn,帮助也说Timer_tick不能保证在网络上正确运用!
      

  3.   

    1.需要知道发送或者接受文件的总大小
    2.需要知道发送或传输的数据量
    3.设置timer(推荐用System.Thread中的timer)定时进行数据处理。
    4.推荐用在timer定时的处理中用事件抛出
    5.在Progressbar中异步处理
      

  4.   

    yuqingjiang,数据我都能得到,但就是不能刷新到Label控件上去!你说的是不是用多线程?
      

  5.   

    那么使用System.Thread.Timer类呢?
    用这个Timer来管理使Label显示的那个线程!!
      

  6.   

    yuqingjiang,你有没有例子让我参考?我做这个好久了~!
      

  7.   

    对要用多线程+异步调用MSDN下有一个STA程序你可以去看看,还要看看Timer的例子等会我再给例子吧,很长现在没有时间整理
      

  8.   

    是的,我现在做一个传输文件的程序,程序中每个时候传了多小byte我都知道,我想把传输的bytes实时显示在Label控件上,却不行,为何Progress就可以呢?
      

  9.   

    如果用System.Threading.Timer 来处理,我好像不能去操作Label控件了。
    \
      

  10.   

    这个类模拟的是数据的接受和处理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);

    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;
    }
    }
    }
      

  11.   

    我是在客户端的Label控件上显示的!
      

  12.   


    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();
    }