简体   繁体   English

C#中的线程问题(字段初始化器无法引用非静态字段,等等)

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

Im having a quite annoying problem with threading in C#. 我在C#中有一个非常烦人的问题。 I get the error "A field initializer cannot reference the non-static field, method, or property 'Scraper.Form1.scrapeStart()'" When using this code: 使用此代码时,出现错误“字段初始化程序无法引用非静态字段,方法或属性'Scraper.Form1.scrapeStart()'”:

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(""); 我意识到,如果我将函数scrapeStart设为静态,它将可以正常工作,但这将使appendOutput(“”); and button1.Enabled = true throw errors. 和button1.Enabled = true抛出错误。 And if I put the new Thread in where its started (button1_Click) then it cannot be aborted in button2_Click. 而且,如果我将新线程放在其开始位置(button1_Click),则无法在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. 我对C#有点了解,所以我可能做错了所有可怕的事情,或者可能只是一个小问题。 But either way, could someone please help me? 但是无论哪种方式,有人可以帮我吗?

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 . 这里有点引人注意的是, this引用是隐式的-您正在使用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 请遵守.NET命名约定
  • You'll also need to fix the threading parts of scrapeStart , which shouldn't be accessing UI elements directly 需要修复scrapeStart的线程部分,该部分不应直接访问UI元素。

I think you need to use InvokeRequired. 我认为您需要使用InvokeRequired。

public void scrapeStart()
{
    if (InvokeRequired)
    {
    this.Invoke(new Action(scrapeStart));
    return;
    }
    Button button1 = new Button();
    appendOutput("");
    button1.Enabled = true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 asp.net c# - 字段初始值设定项不能引用非静态字段,方法或属性 - asp.net c# - A field initializer cannot reference the non-static field, method or property 字段初始值设定项不能引用非静态字段,方法或属性 - A field initializer cannot reference the non-static field, method, or property 字段初始值设定项不能引用非静态字段,方法或属性 - A field initializer cannot reference the non-static field, method or property 字段初始值设定项不能引用非静态字段,方法或属性吗? .1 - A field initializer cannot reference the non-static field, method, or property? .1 字段初始值设定项不能引用非静态字段、方法或属性? - A field initializer cannot reference the non-static field, method, or property? 字段初始值设定项无法引用非静态字段,方法或属性5 - A field initializer cannot reference the non-static field, method, or property 5 字段初始值设定项不能引用非静态字段 - Field initializer cannot reference non-static field 字段初始值设定项不能引用非静态字段,方法或属性 - A field initializer cannot reference the non-static field, method, or property 字段初始值设定项不能引用非静态字段方法或属性 - A field initializer cannot reference the non-static field method or property 属性初始化程序无法引用非静态字段 - Property Initializer cannot reference non-static field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM