Im having a quite annoying problem with threading in C#. I get the error "A field initializer cannot reference the non-static field, method, or property 'Scraper.Form1.scrapeStart()'" When using this code:
public partial class Form1 : Form
{
public Thread scrape = new Thread(() => scrapeStart()); //This is where the error happens
public About about = new About();
public Form1()
{
InitializeComponent();
}
public void appendOutput(String s)
{
output.AppendText(s);
output.SelectionStart = output.Text.Length;
output.ScrollToCaret();
output.Refresh();
}
public void scrapeStart(){
Button button1 = new Button();
appendOutput("");
button1.Enabled = true;
}
private void button3_Click(object sender, EventArgs e)
{
about.ShowDialog();
}
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
scrape.Start();
}
private void button2_Click(object sender, EventArgs e)
{
scrape.Abort();
button1.Enabled = true;
}
}
I realize that if I made the function scrapeStart static it would work, but that would make appendOutput(""); and button1.Enabled = true throw errors. And if I put the new Thread in where its started (button1_Click) then it cannot be aborted in button2_Click.
Im a bit knew to C# so I may have either done everything horribly wrong, or it may just be a small problem. But either way, could someone please help me?