繁体   English   中英

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

[英]C# member cannot be accessed with an instance reference

所以我有这些变量

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)));

我收到此错误:

错误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构造函数,并且调用该方法为时过早,并传递结果( 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 };
}

现在,该线程有了您的搜索函数的委托,并在参数上加上了闭包-如果您不熟悉线程和闭包,则需要阅读它们。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM