Ten*_*rei 2 c# static multithreading private member
所以我有这些变量
List<string> files, images = new List<string>();
string rootStr;
Run Code Online (Sandbox Code Playgroud)
而这个线程功能
private static int[] thread_search(string root,List<string> files, List<string> images)
Run Code Online (Sandbox Code Playgroud)
但是当我尝试启动线程时:
trd = new Thread(new ThreadStart(this.thread_search(rootStr,files,images)));
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
错误1成员'UnusedImageRemover.Form1.thread_search(string,System.Collections.Generic.List,System.Collections.Generic.List)'无法使用实例引用访问; 使用类型名称来限定它而不是E:\ Other\Projects\UnusedImageRemover\UnusedImageRemover\Form1.cs 149 46 UnusedImageRemover
你能告诉我我做错了什么吗?
你有一个静态方法,这意味着它不属于一个实例.this是指当前实例,但由于它是静态的,因此没有意义.
只要删除this.,你应该是好的.
编辑
删除this.会给你一个不同的例外.您应该将void委托传递给ThreadStart构造函数,并且过早地调用该方法,并传入result(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 };
}
Run Code Online (Sandbox Code Playgroud)
现在,线程有一个代表你的搜索功能,关闭参数 - 如果你不熟悉它们,你会想要读取线程和闭包.