G scerri

G scerri

  • NA
  • 3
  • 2.4k

Threads

Jan 11 2011 1:06 PM
i am a beginner to C# i am trying to type some code for threading, and now i came across this error:"A field initializer cannot reference the non-static field, method, or property 'Thread_test.Form1.task()' "

and here is my code in c#:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;


namespace Thread_test
{
    public partial class Form1 : Form
    {
            Thread process = new Thread(new ThreadStart(task));
        

        public void task()
        {
            int a = 0;
            for (; ; )
            {
                a = a + 1;
                textBox1.Text = Convert.ToString(a);
            }
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            process.Start();
        }
    }
}


Thanks for your help !-)

Answers (2)