gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 08:12:53 24 4
gpt4 key购买 nike

我在 C# 中的线程有一个非常烦人的问题。使用此代码时,我收到错误“字段初始化程序无法引用非静态字段、方法或属性'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;
}
}

我意识到如果我将函数 scrapeStart 设为静态它会起作用,但这会使 appendOutput("");和 button1.Enabled = true 抛出错误。如果我将新线程放在它开始的位置(button1_Click),那么它不能在 button2_Click 中中止。

我对 C# 有点了解,所以我可能做的所有事情都非常错误,或者这可能只是一个小问题。但无论哪种方式,有人可以帮助我吗?

最佳答案

这实际上与线程无关。如果你写,你会看到完全相同的问题:

public class Foo
{
int x = 10;
int y = x;
}

或者更清楚:
public class Bar
{
object obj = this;
}
this引用是隐式的 - 您正在创建一个目标为 this 的委托(delegate).

解决方案就是将赋值放入构造函数中:
public Thread scrape;
public About about = new About();
public Form1()
{
InitializeComponent();
scrape = new Thread(scrapeStart);
}

作为旁白:
  • 请不要使用公共(public)领域!
  • 请遵守 .NET 命名约定
  • 您还需要修复 scrapeStart 的线程部分。 ,不应该直接访问 UI 元素
  • 关于c# - C# 中的线程问题(字段初始化程序无法引用非静态字段等),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12659551/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com