我自己的一个读取数据的方法执行时间太长,想用进度条来控制。当方法执行完成,进度条走完。该怎么做?
看了愚翁专栏
http://blog.csdn.net/knight94/archive/2006/05/27/757351.aspx
但是不明白怎么和自己的方法联系起来。
比如如
   private void waitfor()
        {
            for (int i = 0; i < 100; i++)
            {
                Thread.Sleep(1000);
            }
        }
怎么和那个进度条frmProcessBar 联系起来。
希望有人能指点一下,谢谢

解决方案 »

  1.   

       private void waitfor()
            {
    frmProcessBar frm = new frmProcessBar();
                for (int i = 0; i < 100; i++)
                {
    frm.进度条.value += 1;
    或者
    frm.进度条.value=i;
                    Thread.Sleep(1000);
                }
    frm.Close();
            }
      

  2.   

    private void waitfor()
            {
    frmProcessBar frm = new frmProcessBar();
    frm.Show();
    frm.Refresh();
                for (int i = 0; i < 100; i++)
                {
    frm.进度条.value += 1;
    或者
    frm.进度条.value=i;
                    Thread.Sleep(1000);
                }
    frm.Close();
            }
      

  3.   

    bitpolar(日落雁行斜) 
    不好意思,可能是我没说清楚,
    private void waitfor()
    只是一个例子。
    我觉得应该是新建一个委托,把private void waitfor()单独启动一个线程,线程开始,进度条开始滚动,然后线程结束,进度条消失.而不是改写waitfor()方法内部。
      

  4.   

    waitfor()单独启动一个线程 
    让这个线程轮寻工作线程的执行情况应该可以把
      

  5.   

    要看费时操作能否分解, 假如可以分解为循环.
    把费时操作用委托异步调用, 在循环中更新进度条.如果不行, 还是把费时操作用委托异步调用, 这样不阻塞线程. 然后进度条循环显示
    (像windows启动一样).
      

  6.   

    //想法如下,对委托也是半懂不懂,高手看有没有错误,指点一下
    //为本类增加如下这3个成员    
    private frmProcessBar myProcessBar = null;//进度条
    private delegate bool IncreaseHandle( int nValue );//委托,其实有点类似C++中的typedef
    private IncreaseHandle myIncrease = null;//用来调用进度条类中方法
    //为本类增加一个方法,这个方法被线程调用,用来打开进度条
        /// <summary>
        /// Open process bar window
        /// </summary>
        private void ShowProcessBar()
        {
            myProcessBar = new frmProcessBar();
            // Init increase event
            myIncrease = new IncreaseHandle( myProcessBar.Increase );
            myProcessBar.ShowDialog();
            myProcessBar = null;
        }
    //你的线程大概应该如下:
       private void waitfor()
       {        
            MethodInvoker mi = new MethodInvoker( ShowProcessBar );
            this.BeginInvoke( mi );//打开进度条        for (int i = 0; i < 100; i++)
            {
               //调用进度条类中的Increase方法。
               //进度条类中还可以有其他的方法,调用过程基本一致。
               //方法形式不一样时,需要定义新的委托(delegate )
               objReturn = this.Invoke( this.myIncrease, 
                                                new object[]{ 2 } );
                Thread.Sleep(1000);
            }
        }//启动线程
        Thread thdSub = new Thread( new ThreadStart( waitfor) );
        thdSub.Start(); 
      

  7.   

    如果是Vs2005的话,用backgroundWorker来做吧,很简单,Msdn上就有例子,Vs2003默认允许线程间互相访问控件,Vs2005默认就不允许了。用backgroundWorker来做,可以解决UI控制的一些问题,不用担心因主程序运算量较大,而导致进度条半天不动。using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Threading;
    using System.Windows.Forms;namespace BackgroundWorkerExample
    {
        public class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
                // Do not access the form's BackgroundWorker reference directly.
                // Instead, use the reference provided by the sender parameter.
                BackgroundWorker bw = sender as BackgroundWorker;            // Extract the argument.
                int arg = (int)e.Argument;            // Start the time-consuming operation.
                e.Result = TimeConsumingOperation(bw, arg);            // If the operation was canceled by the user, 
                // set the DoWorkEventArgs.Cancel property to true.
                if (bw.CancellationPending)
                {
                    e.Cancel = true;
                }
            }        // This event handler demonstrates how to interpret 
            // the outcome of the asynchronous operation implemented
            // in the DoWork event handler.
            private void backgroundWorker1_RunWorkerCompleted(
                object sender, 
                RunWorkerCompletedEventArgs e)
            {   
                if (e.Cancelled)
                {
                    // The user canceled the operation.
                    MessageBox.Show("Operation was canceled");
                }
                else if (e.Error != null)
                {
                    // There was an error during the operation.
                    string msg = String.Format("An error occurred: {0}", e.Error.Message);
                    MessageBox.Show(msg);
                }
                else
                {
                    // The operation completed normally.
                    string msg = String.Format("Result = {0}", e.Result);
                    MessageBox.Show(msg);
                }
            }        // This method models an operation that may take a long time 
            // to run. It can be cancelled, it can raise an exception,
            // or it can exit normally and return a result. These outcomes
            // are chosen randomly.
            private int TimeConsumingOperation( 
                BackgroundWorker bw, 
                int sleepPeriod )
            {
                int result = 0;            Random rand = new Random();            while (!this.backgroundWorker1.CancellationPending)
                {
                    bool exit = false;                switch (rand.Next(3))
                    {
                        // Raise an exception.
                        case 0:
                        {
                            throw new Exception("An error condition occurred.");
                            break;
                        }                    // Sleep for the number of milliseconds
                        // specified by the sleepPeriod parameter.
                        case 1:
                        {
                            Thread.Sleep(sleepPeriod);
                            break;
                        }                    // Exit and return normally.
                        case 2:
                        {
                            result = 23;
                            exit = true;
                            break;
                        }                    default:
                        {
                            break;
                        }
                    }                if( exit )
                    {
                        break;
                    }
                }            return result;
            }        private void startBtn_Click(object sender, EventArgs e)
            {
                this.backgroundWorker1.RunWorkerAsync(2000);
            }        private void cancelBtn_Click(object sender, EventArgs e)
            {
                this.backgroundWorker1.CancelAsync();
            }        /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;        /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }        #region Windows Form Designer generated code        /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
                this.startBtn = new System.Windows.Forms.Button();
                this.cancelBtn = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // backgroundWorker1
                // 
                this.backgroundWorker1.WorkerSupportsCancellation = true;
                this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);
                this.backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);
                // 
                // startBtn
                // 
                this.startBtn.Location = new System.Drawing.Point(12, 12);
                this.startBtn.Name = "startBtn";
                this.startBtn.Size = new System.Drawing.Size(75, 23);
                this.startBtn.TabIndex = 0;
                this.startBtn.Text = "Start";
                this.startBtn.Click += new System.EventHandler(this.startBtn_Click);
                // 
                // cancelBtn
                // 
                this.cancelBtn.Location = new System.Drawing.Point(94, 11);
                this.cancelBtn.Name = "cancelBtn";
                this.cancelBtn.Size = new System.Drawing.Size(75, 23);
                this.cancelBtn.TabIndex = 1;
                this.cancelBtn.Text = "Cancel";
                this.cancelBtn.Click += new System.EventHandler(this.cancelBtn_Click);
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(183, 49);
                this.Controls.Add(this.cancelBtn);
                this.Controls.Add(this.startBtn);
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);        }        #endregion        private System.ComponentModel.BackgroundWorker backgroundWorker1;
            private System.Windows.Forms.Button startBtn;
            private System.Windows.Forms.Button cancelBtn;
        }    public class Program
        {
            private Program()
            {
            }        /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.Run(new Form1());
            }
        }
    }
      

  8.   

    上面的例子是转贴Msdn的,不是自己写的,你跟踪调试几次,应当就知道是怎么回事儿了!
      

  9.   

    最后一个例子我怎么走也进步去case 1:
                            {
                                Thread.Sleep(sleepPeriod);
                                break;
                            }
    所以进度条的显示根本看不到!