自己做了一个进度条控件,在主线程中OK,在分线程中用Invoke调用之方法,却没有任何绘制反应。
   public partial class ProcessBarEx : UserControl
    {
        public ProcessBarEx()
        {
            InitializeComponent();
            maximum = 100;
            minimum = 0;
            value = 0;
            borderColor = Color.Gray;
            barColor = Color.Blue;
        }
        int maximum;
        [Description("获取或设置控件范围的最大值")]
        [Category("行为")]
        public int Maximum
        {
            get
            { return maximum; }
            set
            {
                if (value > minimum)
                {
                    maximum = value;
                }
            }
        }        int minimum;
        [Description("获取或设置控件范围的最小值")]
        [Category("行为")]
        public int Minimum
        {
            get
            { return minimum; }
            set
            {
                if (value < maximum)
                {
                    minimum = value;
                }
            }
        }        int value;
        [Description("获取或设置控件范围的当前值")]
        [Category("行为")]
        public int Value
        {
            get
            { return value; }
            set
            {
                if (value >= minimum && value <= maximum)
                {
                    this.value = value;
                    //this.Invalidate();
                    //this.Update();
                    this.Refresh();
                }
            }
        }        Color borderColor;
        [Description("获取或设置控件边框颜色")]
        [Category("外观")]
        public Color BorderColor
        {
            get
            { return borderColor; }
            set
            {
                borderColor = value;
            }
        }        Color barColor;
        [Description("获取或设置控件进度条颜色")]
        [Category("外观")]
        public Color BarColor
        {
            get
            { return barColor; }
            set
            { 
                barColor = value;
            }
        }        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            SolidBrush br = new SolidBrush(this.BackColor);
            SolidBrush ba = new SolidBrush(this.barColor);            e.Graphics.FillRectangle(br,e.ClipRectangle);            e.Graphics.DrawRectangle(System.Drawing.Pens.Gray, e.ClipRectangle.X, e.ClipRectangle.Y,
                e.ClipRectangle.Width-32,e.ClipRectangle.Height-1);            int w = (this.value - this.minimum) * (this.Width - 32) / this.maximum;            //e.Graphics.FillRectangle(ba,1,2, w, this.Height - 4);            LinearGradientBrush upBrush = new LinearGradientBrush(
                new Point(10, 0),
                new Point(10, this.Height / 2),
                Color.FromArgb(224, 255, 224),
                Color.FromArgb(0, 153, 0));            LinearGradientBrush downBrush = new LinearGradientBrush(
                new Point(10, 0),
                new Point(10, this.Height / 2),
                Color.FromArgb(0, 153, 0),
                Color.FromArgb(224, 255, 224));            e.Graphics.FillRectangle(upBrush, 1, 2, w, this.Height / 2 - 2);
            e.Graphics.FillRectangle(downBrush, 1, this.Height / 2, w, this.Height / 2 - 2);
            int h;
            if ((this.Height - this.Font.Size) > 2)
            {
                h = (int)(this.Height - (int)this.Font.Size) / 2-1;
            }
            else
            { h = 1; }            e.Graphics.DrawString(string.Format("{0:d}%",(this.value - this.minimum) * 100 / this.maximum) ,
                this.Font, System.Drawing.Brushes.Black, this.Width - 30,h);            br.Dispose();
            ba.Dispose();
            upBrush.Dispose();
            downBrush.Dispose();
        }
    }//end class
在分线程中调用:        //Schedule是ProcessBarEx对象的引用
        public delegate void ScheduleHandler(int Schedule);        public void ChangeSchedule(int Schedule)
        {
            this.Schedule.Value = Schedule;
        }        ScheduleHandler sh = new ScheduleHandler(ChangeSchedule);        this.Schedule.Invoke(sh, new object[] { ... });a
主线程中设置Value属性,结果正常。分线程中调用Value属性,毫无反应。我把ProcessBarEx换成系统自带的进度条控件,则一切正常。(但我不想用系统自带的进度条控件) 大家帮我分析下原因,谢谢!

解决方案 »

  1.   

    我写了一个测试程序,在一个子线程里面是没有问题的:
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();            //创建一个线程
                Thread thread = new Thread(new ThreadStart(BeginThread));
                thread.Start();
            }        public void BeginThread()
            {
                int i = 0;
                while (true)
                {
                    Thread.Sleep(500);
                    i++;
                    this.Invoke(new ScheduleHandler(ChangeSchedule), i);
                    if (i == 100)
                        i = 0;
                }
            }        public delegate void ScheduleHandler(int Schedule);        public void ChangeSchedule(int Schedule)
            {
                this.Schedule.Value = Schedule;        }
        }
      

  2.   

    是不是因为我的控件继承自UserControl呢 ?
      

  3.   

    我的做成DLL后源码就丢失了.不好意思,而且我是用VB写的.
    http://dylike.czdown.com/love1.html
    就是这个样子,打开网页后稍等片刻刷新即可看到控件.WINFORM的,但是可以用在网页上的.
      

  4.   

    或者你可以通过网页源文件下载这个DLL,然后反编译获取源代码.
      

  5.   

    我用的就是你写的ProcessBarEx,你可以和你的代码比较一下,或者贴上你线程创建和执行的代码出来。我在例子中虽然写了this.Invoke,但是this.Schedule.Invoke也是没有问题的。注,应该是Progress而不是Process吧?!
      

  6.   

    我英文单词记错了,不过这并不影响控件工作。问题已解决,结果令人汗颜啊,原来我主线程中设定的是当进度条100%后就隐藏,下次使用时再show。但时间一长,我竟把之当做理所当然,分线程中忘了先show,当然就没任何反应了