So i have these variables
List<string> files, images = new List<string>();
string rootStr;
And this threaded function
private static int[] thread_search(string root,List<string> files, List<string> images)
But when I try to start the thread:
trd = new Thread(new ThreadStart(this.thread_search(rootStr,files,images)));
I get this error:
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
Could you please tell me what I'm doing wrong?
You've got a static method, meaning it does not belong to an instance. this
refers to the current instance, but since it's static, that doesn't make sense.
Just remove the this.
, and you should be good.
EDIT
Removing this.
gets you a different exception. You are supposed to pass in a void
delegate into the ThreadStart
constructor, and you are invoking the method too early, and passing in the result (int[]
). You can pass in a lambda instead, like:
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 };
}
Now the thread has a delegate to your search function, with a closure on the parameters - you'll want to read up on threads and closures if you're not familiar with them already.