无法使用实例引用访问C#成员
所以我有这些变量无法使用实例引用访问C#成员,c#,multithreading,static,private,member,C#,Multithreading,Static,Private,Member,所以我有这些变量 List<string> files, images = new List<string>(); string rootStr; 我得到这个错误: 错误1成员“UnusedImageRemover.Form1.thread\u搜索(字符串, System.Collections.Generic.List, 无法使用“”访问System.Collections.Generic.List“” 实例引用;用类型名限定它 而是E:\Other\Projects
List<string> files, images = new List<string>();
string rootStr;
我得到这个错误:
错误1成员“UnusedImageRemover.Form1.thread\u搜索(字符串,
System.Collections.Generic.List,
无法使用“”访问System.Collections.Generic.List“”
实例引用;用类型名限定它
而是E:\Other\Projects\UnusedImageRemover\UnusedImageRemover\Form1.cs 149 46 UnusedImageRemover
您能告诉我我做错了什么吗?您有一个静态方法,这意味着它不属于实例
此
引用当前实例,但由于它是静态的,因此没有意义
只需删除这个。
,您就应该很好了
编辑
删除此。会导致不同的异常。您应该将一个void
委托传递到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 };
}
static void Main(字符串[]args){
列表文件=新列表(),图像=新列表();
字符串rootStr=“”;
var trd=新线程(新线程开始(()=>线程搜索(rootStr、文件、图像));
trd.Start();
}
私有静态int[]线程搜索(字符串根、列表文件、列表图像{
返回新的[]{1,2,3};
}
现在,线程有了一个对搜索函数的委托,并在参数上有一个闭包-如果您还不熟悉线程和闭包,您将需要了解它们。
trd=new thread(newThreadStart(thread_search(rootStr,files,images)))
这会导致以下错误:需要方法名@Joe。现在,由于调用该方法太快,您将遇到另一个问题。我将在此添加一个编辑,并提供一些帮助。
trd = new Thread(new ThreadStart(this.thread_search(rootStr,files,images)));
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 };
}