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 WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private Thread myThread = new Thread(new ThreadStart(runWork));  //错误 1 字段初始值设定项无法引用非静态字段、方法或属性“WindowsApplication1.Form1.runWork()” D:\测试\测试例程(C#)\WindowsApplication1\Form1.cs 19 46 WindowsApplication1        private delegate void MyInvoke(int percent);
        private MyInvoke showResult = new MyInvoke(showInvoke);  //错误 1 字段初始值设定项无法引用非静态字段、方法或属性“WindowsApplication1.Form1.runWork()” D:\测试\测试例程(C#)\WindowsApplication1\Form1.cs 19 46 WindowsApplication1
        private void runWork()
        {
            for(int i = 0; i< 100; i++)
            {
                BeginInvoke(showResult, i);
                Thread.Sleep(100);
            }
        }        private void showInvoke(int percent)
        {
            textBoxPercentage.Text = percent + "%";
            progressBarPercentage.Value = percent;
            if(percent == 100)
            {
                MessageBox.Show("任务完成!");
                myThread.Abort();
            }
        }        private void Form1_Load(object sender, EventArgs e)
        {
            progressBarPercentage.Minimum = 0;
            progressBarPercentage.Maximum = 100;
        }        private void buttonStart_Click(object sender, EventArgs e)
        {
            myThread.Start();
        }
    }
}

解决方案 »

  1.   

    private Thread myThread = new Thread(new ThreadStart(runWork));  放到
    private void Form1_Load(object sender, EventArgs e)中
      

  2.   

     public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();myThread=new Thread(new ThreadStart(runWork)); 
     showResult=new MyInvoke(showInvoke); 
            }        private Thread myThread =null;  
            private delegate void MyInvoke(int percent);
            private MyInvoke showResult =null;        private void runWork()
            {
                for(int i = 0; i< 100; i++)
                {
                    BeginInvoke(showResult, i);
                    Thread.Sleep(100);
                }
            }        private void showInvoke(int percent)
            {
                textBoxPercentage.Text = percent + "%";
                progressBarPercentage.Value = percent;
                if(percent == 100)
                {
                    MessageBox.Show("任务完成!");
                    myThread.Abort();
                }
            }        private void Form1_Load(object sender, EventArgs e)
            {
                progressBarPercentage.Minimum = 0;
                progressBarPercentage.Maximum = 100;
            }        private void buttonStart_Click(object sender, EventArgs e)
            {
                myThread.Start();
            }
        }