我有一个工程,里面有MainForm.cs窗体,一个ProgressForm.cs窗体,一个类Class1其中有四个方法RunA,FunB,FunC,FunD
MainForm上有一个按钮btn,顺序执行四个方法,现在要求执行四个方法前先弹出ProgressForm窗体,显示进度条,要求MainForm不可操作
四个方法各占进度条进度的25%,四个方法中每个方法里面都是循环,要求根据循环次数与25%的进度计算,动态显示进度,四个方法操作完之后
关闭ProgressForm如进度长度是100,那么主法RunA里面循环88次,那么根据进度条的25%+88次来计算,也就是说每循环一次大约就是进度条四分之一/88

解决方案 »

  1.   

    LZ这哪来的多线程?既然有四个方法各占25%,A中有88次,那就是88/25步长为1%,这很好做呀做个全局,求出步长1%为88/25次循环,以次类推
      

  2.   

    楼主如果你的需求是,先show 出代 progressBar 的窗口在调用
    若干函数、
    主窗口不能点这样的话根本无需线程、下面代码即可public partial class MainForm : Form
    {
    public MainForm()
    {
    InitializeComponent();
    }
    ProgressForm _progressForm = null;
    private void button1_Click(object sender, EventArgs e)
    {
    /*
    把 _progressForm.progressBar1 改为 public  
    */
    try
    {

    _progressForm = new ProgressForm();
    _progressForm.Owner= this;
    _progressForm.Show();

    this.Enabled = false;
    Run(100);
    Run(200);
    Run(88);
    Run(60);

    }
    finally
    {

    if(_progressForm!=null)
    {
    _progressForm.Dispose();
    }
    this.Enabled = true;
    }

    }

    public void Run(int count)
    {
    /*由于这些函数都是一样的所以写一个、作为演示*/
    int p = _progressForm.progressBar1.Value;
    //int count = 100;
    float f = count / 25F; for (int i = 0; i < count; i++)
    {
    _progressForm.progressBar1.Value = p + (int)(i / f);
    Application.DoEvents(); //可以不加这个
    Thread.Sleep(10); //可以不加这个
    }
    }

    }
      

  3.   

    ProgressForm 里无需代码!就放置一个 progressBar 设置好样式就可以了
      

  4.   

    progressBar 改为 public  
      

  5.   

    看标题需要多线程..看内容不需要..同意FlashElf
    不过看在200分的情况下还是写了一个多线程同步的.
      

  6.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using Microsoft.Build.Tasks.Deployment.ManifestUtilities;
    using System.Threading;namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {        Thread thread1;
            Thread thread2;
            Thread thread3;
            Thread thread4;        delegate void VOID();
            
            Form2 form2 = new Form2();        bool end1 = false;
            bool end2 = false;
            bool end3 = false;
            bool end4 = false;        public Form1()
            {
                InitializeComponent();
                thread1 = new Thread(new ThreadStart(ThreadRun4));
                thread2 = new Thread(new ThreadStart(ThreadRun1));
                thread3 = new Thread(new ThreadStart(ThreadRun2));
                thread4 = new Thread(new ThreadStart(ThreadRun3));        }        void ThreadEnd()
            {
                if (end1 && end2 && end3 && end4)
                {
                    
                    form2.Close();
                }
            }        void ShowForm2()
            {
                if (form2.Visible != true)
                {
                    form2.Show(this);
                }
            }
            
            float plan1 = 0;
            float plan2= 0;
            float plan3 = 0;
            float plan4 = 0;
            void SetPlan()
            {
                float temp = (plan1 + plan2 + plan3 + plan4) * 10000 * 0.25f;            form2.progressBar1.Value = (int)Math.Round(temp);            
                form2.progressBar2.Value = (int)Math.Round(plan1 * 10000);
                form2.progressBar3.Value = (int)Math.Round(plan2 * 10000);
                form2.progressBar4.Value = (int)Math.Round(plan3 * 10000);
                form2.progressBar5.Value = (int)Math.Round(plan4 * 10000);
            }                private void button1_Click(object sender, EventArgs e)
            {
                thread1.Start();
                thread2.Start();
                thread3.Start();
                thread4.Start();
            }        Random random = new Random();        void ThreadRun4()
            {
                int maxCount;
                int sleep;
                lock (random)
                {
                    maxCount = random.Next(10, 100);
                    sleep = random.Next(100, 500);
                }
                this.Invoke(new VOID(ShowForm2));            
                for (int a = 0; a <= maxCount;a++ )
                {
                    Thread.Sleep(sleep);
                    plan4 = (float)a / maxCount;
                    this.Invoke(new VOID(SetPlan));
                }
                end4 = true;
                this.Invoke(new VOID(ThreadEnd));
            }        void ThreadRun1()
            {            int maxCount;
                int sleep;
                lock (random)
                {
                    maxCount = random.Next(10, 100);
                    sleep = random.Next(100, 500);
                }
                this.Invoke(new VOID(ShowForm2));            
                for (int a = 0; a <= maxCount; a++)
                {
                    Thread.Sleep(sleep);
                    plan1 = (float)a / maxCount;
                    this.Invoke(new VOID(SetPlan));
                }
                end1 = true;
                this.Invoke(new VOID(ThreadEnd));
            }
            void ThreadRun2()
            {            int maxCount;
                int sleep;
                lock (random)
                {
                    maxCount = random.Next(10, 100);
                    sleep = random.Next(100, 500);
                }
                this.Invoke(new VOID(ShowForm2));            
                
                for (int a = 0; a <= maxCount; a++)
                {
                    Thread.Sleep(sleep);
                    plan2 = (float)a / maxCount;
                    this.Invoke(new VOID(SetPlan));
                }
                end2 = true;
                this.Invoke(new VOID(ThreadEnd));
            }
            void ThreadRun3()
            {
                int maxCount;
                int sleep;
                lock (random)
                {
                    maxCount = random.Next(10, 100);
                    sleep = random.Next(100, 500);
                }
                this.Invoke(new VOID(ShowForm2));            
                
                for (int a = 0; a <= maxCount; a++)
                {
                    Thread.Sleep(sleep);
                    plan3 = (float)a / maxCount;
                    this.Invoke(new VOID(SetPlan));
                }
                end3 = true;
                this.Invoke(new VOID(ThreadEnd));
            }       
            
           
        }
    }namespace WindowsApplication1
    {
        partial class Form1
        {
            /// <summary>
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;        /// <summary>
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }        #region Windows 窗体设计器生成的代码        /// <summary>
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.button1 = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(23, 33);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(75, 23);
                this.button1.TabIndex = 0;
                this.button1.Text = "button1";
                this.button1.UseVisualStyleBackColor = true;
                this.button1.Click += new System.EventHandler(this.button1_Click);
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(284, 264);
                this.Controls.Add(this.button1);
                this.Name = "Form1";
                this.Text = "Form1";
                this.ResumeLayout(false);        }        #endregion        private System.Windows.Forms.Button button1;    }
    }
      

  7.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication1
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
        }
    }namespace WindowsApplication1
    {
        partial class Form2
        {
            /// <summary>
            /// 必需的设计器变量。
            /// </summary>
            private System.ComponentModel.IContainer components = null;        /// <summary>
            /// 清理所有正在使用的资源。
            /// </summary>
            /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }        #region Windows 窗体设计器生成的代码        /// <summary>
            /// 设计器支持所需的方法 - 不要
            /// 使用代码编辑器修改此方法的内容。
            /// </summary>
            private void InitializeComponent()
            {
                this.progressBar1 = new System.Windows.Forms.ProgressBar();
                this.progressBar2 = new System.Windows.Forms.ProgressBar();
                this.progressBar3 = new System.Windows.Forms.ProgressBar();
                this.progressBar4 = new System.Windows.Forms.ProgressBar();
                this.progressBar5 = new System.Windows.Forms.ProgressBar();
                this.SuspendLayout();
                // 
                // progressBar1
                // 
                this.progressBar1.Dock = System.Windows.Forms.DockStyle.Top;
                this.progressBar1.Location = new System.Drawing.Point(0, 0);
                this.progressBar1.Maximum = 10000;
                this.progressBar1.Name = "progressBar1";
                this.progressBar1.Size = new System.Drawing.Size(665, 27);
                this.progressBar1.TabIndex = 0;
                // 
                // progressBar2
                // 
                this.progressBar2.Dock = System.Windows.Forms.DockStyle.Top;
                this.progressBar2.Location = new System.Drawing.Point(0, 27);
                this.progressBar2.Maximum = 10000;
                this.progressBar2.Name = "progressBar2";
                this.progressBar2.Size = new System.Drawing.Size(665, 20);
                this.progressBar2.TabIndex = 1;
                // 
                // progressBar3
                // 
                this.progressBar3.Dock = System.Windows.Forms.DockStyle.Top;
                this.progressBar3.Location = new System.Drawing.Point(0, 47);
                this.progressBar3.Maximum = 10000;
                this.progressBar3.Name = "progressBar3";
                this.progressBar3.Size = new System.Drawing.Size(665, 20);
                this.progressBar3.TabIndex = 2;
                // 
                // progressBar4
                // 
                this.progressBar4.Dock = System.Windows.Forms.DockStyle.Top;
                this.progressBar4.Location = new System.Drawing.Point(0, 67);
                this.progressBar4.Maximum = 10000;
                this.progressBar4.Name = "progressBar4";
                this.progressBar4.Size = new System.Drawing.Size(665, 20);
                this.progressBar4.TabIndex = 3;
                // 
                // progressBar5
                // 
                this.progressBar5.Dock = System.Windows.Forms.DockStyle.Top;
                this.progressBar5.Location = new System.Drawing.Point(0, 87);
                this.progressBar5.Maximum = 10000;
                this.progressBar5.Name = "progressBar5";
                this.progressBar5.Size = new System.Drawing.Size(665, 20);
                this.progressBar5.TabIndex = 4;
                // 
                // Form2
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(665, 108);
                this.Controls.Add(this.progressBar5);
                this.Controls.Add(this.progressBar4);
                this.Controls.Add(this.progressBar3);
                this.Controls.Add(this.progressBar2);
                this.Controls.Add(this.progressBar1);
                this.Name = "Form2";
                this.Text = "Form2";
                this.ResumeLayout(false);        }        #endregion        public System.Windows.Forms.ProgressBar progressBar1;
            public System.Windows.Forms.ProgressBar progressBar2;
            public System.Windows.Forms.ProgressBar progressBar3;
            public System.Windows.Forms.ProgressBar progressBar4;
            public System.Windows.Forms.ProgressBar progressBar5;    }
    }
      

  8.   


    zhiang75, 楼主是用一个进度条,你怎么给弄了5个
      

  9.   

    请问invoke是哪个对象的方法? form?  thread?
      

  10.   

    invoke 同步委托执行方法
    Delete 的方法,Control也有此方法
    一般做法是Control.Invoke(所需封送回窗体主线程处理的委托方法)
    多线程环境里没把握最好用Control.BeginInvoke(),以免线程阻塞看LZ的说法:    我觉得FormLoad时主线程执行方法,在各方法体完成时设置一个成员变量,以用来判断是否执行完毕。new 一个Thread()    弹出PorcessBar,慢慢跑进度条,一个方法结束,运行至25%。最后在主线程里关闭此线程,以前写WindowsForm的时候有写过,有点忘记了,LZ调试下好了。好像当时用了两个Thread才搞定,加主线程三个
      

  11.   

    sorry,上面打错,委托Delegate 而非delete
      

  12.   

    TO:blackboyofsnp
    五个...一起跑...看进度...很有意思..
      

  13.   

    很感谢大家的帮助,这个代码比较难实现,方法里面的循环次数,外界是不知道的,是方法里面其它代码产生的,也就是说,做demo的话,循环次数写在里面
    自己也写了一半(只有一个"load...."字样的dialog窗体,执行完方法,关闭dialog)
    加上进度条的话,实现起来太难了.一会儿发代码让大家评评
      

  14.   

    OperationClass.cspublic class OperationClass
        {
            public void RunA()
            {
                System.Console.WriteLine("Begin RunA");
                System.Threading.Thread.Sleep(2000);
                System.Console.WriteLine("End RunA");
            }        public void RunB()
            {
                System.Console.WriteLine("Begin RunB");
                System.Threading.Thread.Sleep(2000);
                System.Console.WriteLine("End RunB");
            }        public void RunC()
            {
                System.Console.WriteLine("Begin RunC");
                System.Threading.Thread.Sleep(2000);
                System.Console.WriteLine("End RunC");
            }        /*public void RunD(ref int ProgressInt)
            {
                int x = 70;            for (int i = 0; i < x; i++)
                {
                    if (ProgressInt != 70)
                        ProgressInt = 70 / x;                System.Threading.Thread.Sleep(500);
                }
            }*/
        }ProgressForm1.cs
    现在只完成了一半,没有进度条,所以只有一load....字样的label,可以加张gif图片//private IAsyncResult aResult;
            public ProgressForm1()
            {
                //先将主窗体充为隐藏
                this.Visible = false;
                InitializeComponent();
            }        ///// <summary>
            ///// Delegate function to be invoked by main thread
            ///// </summary>
            //private void InvokeFun()
            //{
            //    while (!aResult.IsCompleted)
            //    {        //    }        //    this.Close();
            //}
            ///// <summary>
            ///// Thread function interface
            ///// </summary>
            //private void ThreadFun()
            //{
            //    //Create invoke method by specific function
            //    MethodInvoker mi = new MethodInvoker(this.InvokeFun);
            //    this.BeginInvoke(mi);
            //}        //public void SetIAsyncResult(ref IAsyncResult result)
            //{
            //    aResult = result;
            //}        private void ProgressForm1_Load(object sender, EventArgs e)
            {
                this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
                this.SuspendLayout();
                this.ResumeLayout();            //设窗体可见
                this.Visible = true;            //重绘当前窗体中的控件
                this.Refresh();
            }
      

  15.   

    MainForm.cs
    如果大家不做真实进度的话,可以放一张load的图片,也可以代替,把下面一些代码中的ref int ProgressInt去掉就可以了public partial class MainForm : Form
        {
            private IAsyncResult aResult;
            public MainForm()
            {
                InitializeComponent();
            }        private void btnRun_Click(object sender, EventArgs e)
            {
                int ProgressInt = 0;            ProgressForm1 frm = new ProgressForm1();
                //利用线程回调
                AsyncCallback callback = new AsyncCallback(EndAsync);
                IAsyncResult result = BeginAsync(ref ProgressInt,callback, null);
                //frm.SetIAsyncResult(ref result);
                aResult = result;            MethodInvoker2 mi = new MethodInvoker2(InvokeFun);
                this.BeginInvoke(mi, new object[] { frm, ProgressInt });            frm.ShowDialog();
            }        //声明一个委托
            private delegate void DelegetRun(ref int ProgressInt);
            
            //同步写文件方法
            public static void ThreadProc(ref int ProgressInt) 
            {
                OperationClass oc = new OperationClass();
                oc.RunA();
                //ProgressInt = 10;
                oc.RunB();
                //ProgressInt = 20;
                oc.RunC();
                //ProgressInt = 80;            //oc.RunD(ref ProgressInt);
            }        /// <summary>
            /// 异步开始
            /// </summary>
            public IAsyncResult BeginAsync(ref int ProgressInt,AsyncCallback ac, Object state)
            {
                DelegetRun d = new DelegetRun(ThreadProc);
                IAsyncResult result = d.BeginInvoke(ref ProgressInt,ac, state);
                return result;
            }        /// <summary>
            /// 异步结束
            /// </summary>
            public void EndAsync(IAsyncResult result)
            {
                //DelegetRun d = (DelegetRun)((AsyncResult)result).AsyncDelegate;
                //d.EndInvoke(result);
            }        /// <summary>
            /// Delegate function to be invoked by main thread
            /// </summary>
            private void InvokeFun(ProgressForm1 frm,ref int ProgressInt)
            {
                while (!aResult.IsCompleted)
                {
                    //frm.progressBar1.Value = ProgressInt;
                }            frm.Close();
            }
    }目前只完成这么多,希望高手赐教
      

  16.   

    少一句
     private delegate void MethodInvoker2(ProgressForm1 frm, ref int ProgressInt);