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 ProgressBar
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {
            
        }        //开始下载;
        public void StartDownload() 
        {
            Downloader downloader = new Downloader();
            downloader.onDownLoadProgress += new Downloader.dDownloadProgress(downloader_onDownLoadProgress);
            downloader.Start();
        }        // 开始同步更新;
        void downloader_onDownLoadProgress(long total, long current)
        { 
            if(this.InvokeRequired)
            {
                this.Invoke(new Downloader.dDownloadProgress(downloader_onDownLoadProgress), new object[] {total,current});
            }
            else
            {
                this.progressBar1.Maximum = Convert.ToInt32(total);
                this.progressBar1.Value = Convert.ToInt32(current);
            }
        }        private void button1_Click(object sender, EventArgs e)
        {
            new Thread(new ThreadStart(StartDownload)).Start();
        }        private void button2_Click(object sender, EventArgs e)
        {
            this.progressBar1.Increment(0);
        }
    }
    
    /// <summary>
    /// 开始执行一个下载类;
    /// </summary>
    public class Downloader
    {
        //声明委托;
        public delegate void dDownloadProgress(long total,long current);
        //声明事件;
        public event dDownloadProgress onDownLoadProgress;
        //开始模拟工作;
        public void Start() 
        {
            for (int i = 0; i < 100; i++)
            {
                if (onDownLoadProgress != null)
                {
                    onDownLoadProgress(100, i);
                    Thread.Sleep(10);
                }
            }
        }

    }
}
以上是"jinjazz" 写的一段关于进度条代码,很精典,思路也比较明确;我是一个初学者,想问一下;如果 Start() 是一个较复杂的事务处理过程;不仅是一个for循环,比如有多个循环,怎么办?

解决方案 »

  1.   

    补充一下源代码: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 ProgressBar
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                
            }        //开始下载;
            public void StartDownload() 
            {
                Downloader downloader = new Downloader();
                downloader.onDownLoadProgress += new Downloader.dDownloadProgress(downloader_onDownLoadProgress);
                downloader.Start();
            }        // 开始同步更新;
            void downloader_onDownLoadProgress(long total, long current)
            { 
                if(this.InvokeRequired)
                {
                    this.Invoke(new Downloader.dDownloadProgress(downloader_onDownLoadProgress), new object[] {total,current});
                }
                else
                {
                    this.progressBar1.Maximum = Convert.ToInt32(total);
                    this.progressBar1.Value = Convert.ToInt32(current);
                }
            }        private void button1_Click(object sender, EventArgs e)
            {
                new Thread(new ThreadStart(StartDownload)).Start();
            }        private void button2_Click(object sender, EventArgs e)
            {
                this.progressBar1.Increment(0);
            }
        }
        
        /// <summary>
        /// 开始执行一个下载类;
        /// </summary>
        public class Downloader
        {
            //声明委托;
            public delegate void dDownloadProgress(long total,long current);
            //声明事件;
            public event dDownloadProgress onDownLoadProgress;
            //开始模拟工作;
            public void Start() 
            {
                for (int i = 0; i < 100; i++)
                {
                    if (onDownLoadProgress != null)
                    {
                        onDownLoadProgress(100, i);
                        Thread.Sleep(10);
                    }
                }
            }
        }
    }
      

  2.   

    //开始模拟事务处理过程;
    //如果这个事务处理比较复杂化,怎么和进度条的参数关联?? 
            public void Start() 
            {
                for (int i = 0; i < 100; i++)
                {
                    if (onDownLoadProgress != null)
                    {
                        onDownLoadProgress(100, i);
                        Thread.Sleep(10);
                    }
                }
            }

      

  3.   

    你可以定义两个类似dDownloadProgress 的event,分别控制两个进度条啊
      

  4.   

    Thanks to jinjazz:
    我主要是想实现当用户登录之后,根据用户在系统中的角色,然后
    "初始化" winform ,初始化winform的过程就比较复杂化了,因为
    这个winform中有许多的事务处理过程,想通过一个programebar来
    实现??