考虑:

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //int[] val = { 0, 0};
            int val;
            if (textBox1.Text == "")
            {
                MessageBox.Show("Input any no");
            }
            else
            {
                val = Convert.ToInt32(textBox1.Text);
                Thread ot1 = new Thread(new ParameterizedThreadStart(SumData));
                ot1.Start(val);
            }
        }

        private static void ReadData(object state)
        {
            System.Windows.Forms.Application.Run();
        }

        void setTextboxText(int result)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new IntDelegate(SetTextboxTextSafe), new object[] { result });
            }
            else
            {
                SetTextboxTextSafe(result);
            }
        }

        void SetTextboxTextSafe(int result)
        {
            label1.Text = result.ToString();
        }

        private static void SumData(object state)
        {
            int result;
            //int[] icount = (int[])state;
            int icount = (int)state;

            for (int i = icount; i > 0; i--)
            {
                result += i;
                System.Threading.Thread.Sleep(1000);
            }
            setTextboxText(result);
        }

        delegate void IntDelegate(int result);

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

为什么会出现这个错误?

对于非静态字段、方法或属性,需要一个对象引用


当前回答

您的方法必须是静态的

static void setTextboxText(int result)
{
    if (this.InvokeRequired)
    {
        this.Invoke(new IntDelegate(SetTextboxTextSafe), new object[] { result }); 
    }
    else
    {
        SetTextboxTextSafe(result);
    }
}

其他回答

它看起来像是从静态方法(特别是SumData)调用一个非静态成员(属性或方法,特别是setTextboxText)。你需要:

Make the called member static also: static void setTextboxText(int result) { // Write static logic for setTextboxText. // This may require a static singleton instance of Form1. } Create an instance of Form1 within the calling method: private static void SumData(object state) { int result = 0; //int[] icount = (int[])state; int icount = (int)state; for (int i = icount; i > 0; i--) { result += i; System.Threading.Thread.Sleep(1000); } Form1 frm1 = new Form1(); frm1.setTextboxText(result); } Passing in an instance of Form1 would be an option also. Make the calling method a non-static instance method (of Form1): private void SumData(object state) { int result = 0; //int[] icount = (int[])state; int icount = (int)state; for (int i = icount; i > 0; i--) { result += i; System.Threading.Thread.Sleep(1000); } setTextboxText(result); }

关于此错误的更多信息可以在MSDN上找到。

启动一个运行静态方法SumData的线程。然而,SumData调用SetTextboxText,这不是静态的。因此,您需要一个窗体实例来调用SetTextboxText。

您的方法必须是静态的

static void setTextboxText(int result)
{
    if (this.InvokeRequired)
    {
        this.Invoke(new IntDelegate(SetTextboxTextSafe), new object[] { result }); 
    }
    else
    {
        SetTextboxTextSafe(result);
    }
}

我实际上得到了这个错误,因为我正在检查InnerHtml的一些内容是动态生成的-即一个控件是runat=server。

为了解决这个问题,我不得不在我的方法上删除“static”关键字,它运行得很好。

问题的本质和解决方法是:

using System;

namespace myNameSpace
{
    class Program
    {
        private void method()
        {
            Console.WriteLine("Hello World!");
        }

        static void Main(string[] args)
        {
            method();//<-- Compile Time error because an instantiation of the Program class doesnt exist
            Program p = new Program();
            p.method();//Now it works. (You could also make method() static to get it to work)
        }
    }
}