日期:2014-05-18  浏览次数:21334 次

c# progressBar使用
我要上传一个大文件,由于时间很长,不知道什么时候结束。C#中怎么样使用progressBar能够知道现在上传文件的进度呢?

------解决方案--------------------
C# code

//一个模拟.....也可能不适合你,不知道你的发送文件代码是怎么写的
    private void button3_Click(object sender, EventArgs e)
    {
        Thread thread = new Thread(new ThreadStart(Send));
        thread.Start();
    }

    private void Send()
    {
      int i = 0;
      while (i <= 100)
      {
         //显示进度 信息
        this.ShowPro(i);
        //循环发生文件
         //模拟的 
        i++; //模拟发送多少
        Thread.Sleep(100);
      }
      Thread.CurrentThread.Abort();
    }

    private void ShowPro(int value)
    {
      if (this.InvokeRequired)
      {
        this.Invoke(new ProgressBarShow(ShowPro), value);
      }
      else
      {
        this.progressBar1.Value = value;
        this.label1.Text = value + "%";
      }
    }

------解决方案--------------------
前几天刚用过,修改了一下,参考了一位前辈的例子
http://blog.mylover.cn/2009/02/post-3.html
C# code


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace TestProgressBar
{
    public partial class TestForm : Form
    {
        //线程开始的时候调用的委托
        private delegate void maxValueDelegate(WordEventArgs e);
        //线程执行中调用的委托
        private delegate void nowValueDelegate(WordEventArgs e);
        //线程执行结束调用的委托
        private delegate void endValueDelegate(WordEventArgs e);

        public TestForm()
        {
            InitializeComponent();
        }

        private void btn_Test_Click(object sender, EventArgs e)
        {
            Worker worker = new Worker();

            /// 订阅事件
            worker.OnStartWorkEvent += new WorkEventHandler(this.OnStartWorkEventHandler);
            worker.OnDoWorkEvent += new WorkEventHandler(this.OnDoWorkEventHandler);
            worker.OnEndWorkEvent += new WorkEventHandler(this.OnEndWorkEventHandler);

            Thread thread = new Thread(new ThreadStart(worker.Work));
            thread.Start();  
        }

        /// <summary>
        /// 线程开始事件,设置进度条最大值,需要一个委托来替我完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OnStartWorkEventHandler(object sender, WordEventArgs e)
        {            
            maxValueDelegate max = new maxValueDelegate(SetMaxValue);
            this.Invoke(max, e);
        }

        /// <summary>
        /// 线程执行中的事件,设置进度条当前进度,需要一个委托来替我完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OnDoWorkEventHandler(object sender, WordEventArgs e)
        {           
            nowValueDelegate now = new nowValueDelegate(SetNowValue);
            this.Invoke(now, e);
        }

        /// <summary>
        /// 线程完成事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OnEndWorkEventHandler(object sender, WordEventArgs e)
        {
            endValueDelegate end = new endValueDelegate(SetEndValue);
            this.Invoke(end, e);
        }

        /// <summary>
        /// 被委托调用,专门设置进度条最大值的
        /// </summary>
        /// <param name="maxValue"></param>
        private void SetMaxValue(WordEventArgs e)
        {
            this.probar_Test.Maximum = e.Position;
            lbl_Info.Text = e.Info;
        }

        /// <summary>
        /// 被委托调用,专门设置进度条当前值的
        /// </summary>
        /// <param name="nowValue"></param>
        private void SetNowValue(WordEventArgs e)
        {
            this.probar_Test.Value