gpt4 book ai didi

无法使用实例引用访问 C# 成员

转载 作者:行者123 更新时间:2023-11-30 20:02:53 30 4
gpt4 key购买 nike

所以我有这些变量

List<string> files, images = new List<string>();
string rootStr;

还有这个线程函数

private static int[] thread_search(string root,List<string> files, List<string> images)

但是当我尝试启动线程时:

trd = new Thread(new ThreadStart(this.thread_search(rootStr,files,images)));

我收到这个错误:

Error 1 Member 'UnusedImageRemover.Form1.thread_search(string, System.Collections.Generic.List, System.Collections.Generic.List)' cannot be accessed with an instance reference; qualify it with a type name instead E:\Other\Projects\UnusedImageRemover\UnusedImageRemover\Form1.cs 149 46 UnusedImageRemover

你能告诉我我做错了什么吗?

最佳答案

你有一个静态方法,这意味着它不属于一个实例。 this 引用当前实例,但由于它是静态的,所以没有意义。

只要删除this.,你就可以了。

编辑

删除 this. 会得到一个不同的异常。您应该将 void 委托(delegate)传递给 ThreadStart 构造函数,但您过早地调用了该方法,并传递了结果 (int[])。您可以改为传入 lambda,例如:

static void Main(string[] args) {
List<string> files = new List<string>(), images = new List<string>();
string rootStr = "";

var trd = new Thread(new ThreadStart(() => thread_search(rootStr, files, images)));
trd.Start();
}

private static int[] thread_search(string root, List<string> files, List<string> images {
return new[] { 1, 2, 3 };
}

现在线程有一个委托(delegate)给您的搜索函数,并在参数上有一个闭包 - 如果您还不熟悉线程和闭包,您将需要阅读它们。

关于无法使用实例引用访问 C# 成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16023147/

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