processbar用另一个线程去显示主进程的运行情况,C#怎么实现?
对多线程的处理不是很熟悉。请指教。

解决方案 »

  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;
    using System.Diagnostics;namespace BIAD_Auth
    {
        public partial class Form3 : Form
        {
            public Form3()
            {
                InitializeComponent();
            }
            private int pbvalue = 0;        private void button1_Click(object sender, EventArgs e)
            {
                
                progressBar1.Minimum = 0;
                progressBar1.Maximum = 20;
                System.Threading.Thread t = new System.Threading.Thread(new ThreadStart(SyncProcessbar));
                t.Start();
                for( int i= 0;i<20; i++)
                {
                    Thread.Sleep(10000);
                    pbvalue++;
                }
            }
            private void SyncProcessbar()
            {
                for (int j = 0; j < 200; j++)
                {
                    Thread.Sleep(1000);
                    Trace.WriteLine("pbvalue = " + pbvalue);
                    progressBar1.Value = pbvalue;
                }
            }
        }
    }////////////
    progressBar1.Value = pbvalue; //这行显示如下错误:Cross-thread operation not valid: Control 'progressBar1' accessed from a thread other than the thread it was created on.
      

  2.   

    你是指progressBar1这个控件定义,也放到辅助线程里去?