Threading problems in C# (A field initializer cannot reference the non-static field, ect)

北战南征 提交于 2019-12-10 12:19:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!