我有一个程序,要重复不断的用GDI+画图,而且在画图的过程要执行很多函数。
是这样的,画图我在panel1_Paint()事件中写入画图代码(里面有函数,图像随时改变),
然后用this.panel1.Invalidate();实现图像变化。
我在窗体中加入一个按钮,点击按钮的时候开始画图,并且执行很多函数,这样图像不会显示。
我想加入一个线程,把一些函数都放到线程中执行。
比如,我想实现如下操作的同时画图。
  while(表达式)
  {
    函数1();
    函数2();
    函数3();
    this.panel1.Invalidate();//函数x();对图像有影响。
  }
  while是在button按钮事件中。
其中 函数x();都是对数组的操作。
如何使用线程把这个while表达式中的函数包裹起来在后台运行??
如果不用线程,可有其他方法???

解决方案 »

  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 WindowsApplication2
    {
        public partial class Form1 : Form
        {
            private Thread _thread = null;
            public Form1()
            {
                InitializeComponent();
            }
            private void button1_Click(object sender, EventArgs e)
            {
                ThreadStart threadStart = new ThreadStart(threadStart);
                _thread = new Thread(threadStart);
                _thread.Start();
            }
            public void ThreadMothed()
            {
                try
                {
                    X1();
                    X2();
                }
                catch (ThreadAbortException ex)
                {
                    Thread.ResetAbort();            }
                
            }
            public void X1()
            { 
            
            }
            public void X2()
            { 
            
            }
        }
    }
      

  2.   

    //刚才回复没有测试,修改一下
    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 WindowsApplication2
    {
        public partial class Form1 : Form
        {
            private Thread _thread = null;
            public Form1()
            {
                InitializeComponent();
            }
            private void button1_Click(object sender, EventArgs e)
            {
                ThreadStart threadStart = new ThreadStart(ThreadMothed); //这个地方应该些对应的方法
                _thread = new Thread(threadStart);
                _thread.Start();
            }
            public void ThreadMothed()
            {
                try
                {
                    X1();
                    X2();
                }
                catch (ThreadAbortException ex)
                {
                    Thread.ResetAbort();            }
                
            }
            public void X1()
            { 
            
            }
            public void X2()
            { 
            
            }
        }
    }
      

  3.   

    Sample code as follows:
    // in your thread function
    while(表达式)
    {
    函数1();
    函数2();
    函数3();
    this.panel1.Invalidate();//函数x();对图像有影响。
    Thread.Sleep( 20 );// Suspend current sub-thread
    }