When I try to run (debug) the program it says:
Error CS0120 An object reference is required for the non-static field, method, or property 'Form1.richTextBox1'
I want to learn how to use multiple threads in windows forms application.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Synthesis;
using System.Threading;
namespace test
{
public partial class Form1 : Form
{
Thread countdown = new Thread(new ThreadStart(CountDown));
private static SpeechSynthesizer synth = new SpeechSynthesizer();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.AppendText("A button was clicked\r\n");
}
private void button2_Click(object sender, EventArgs e)
{
countdown.Start();
}
public static void CountDown()
{
synth.Speak("Starting!");
for (int i = 10; i >= 0; i--)
{
richTextBox1.AppendText(i + "\r\n");
System.Threading.Tasks.Task.Delay(1000).Wait();
richTextBox1.Clear();
}
}
}
}
public static void CountDown()
changed to public void CountDown()
.
Now when I try to run (debug) the program it says:
Error CS0236 A field initializer cannot reference the non-static field, method, or property 'Form1.CountDown()'