问题
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?
回答1:
This really has nothing to do with threading. You'll see exactly the same problem if you write:
public class Foo
{
int x = 10;
int y = x;
}
Or even more clearly:
public class Bar
{
object obj = this;
}
There's the slight distraction here that the this
reference is implicit - you're creating a delegate with a target of this
.
The solution is just to put the assignment into the constructor:
public Thread scrape;
public About about = new About();
public Form1()
{
InitializeComponent();
scrape = new Thread(scrapeStart);
}
As an aside:
- Please don't use public fields!
- Please adhere to .NET naming conventions
- You'll also need to fix the threading parts of
scrapeStart
, which shouldn't be accessing UI elements directly
回答2:
I think you need to use InvokeRequired.
public void scrapeStart()
{
if (InvokeRequired)
{
this.Invoke(new Action(scrapeStart));
return;
}
Button button1 = new Button();
appendOutput("");
button1.Enabled = true;
}
来源:https://stackoverflow.com/questions/12659551/threading-problems-in-c-sharp-a-field-initializer-cannot-reference-the-non-stat