So, I’m trying to pass an argument to a method that I want to participate in multithreading. So, I wrote code that looks like this:
new Thread (Go(ineedthis)).Start();
Go();
static void Go(string ineedthis)
{
lock (locker)
{
if (!done) { Console.WriteLine ("Done"); done = true; }
}
}
However, I can’t pass the argument ineedthis, because it will give an error when you insert it like I did in the first line. Conversely, if you don’t give an argument when making the thread for the method, it will also give an error.
So, how does one pass an argument to a method when creating a thread?
Thanks!
Note: I just started c# yesterday, so I’m totally new to this. Please explain well so I get more out of it!
EDIT – Errors:
Error 1 The best overloaded method match for 'System.Threading.Thread.Thread(System.Threading.ParameterizedThreadStart)' has some invalid arguments 23 21 test
Error 2 Argument 1: cannot convert from 'method group' to 'System.Threading.ParameterizedThreadStart' 23 32 test
I think you are looking for something more like this:
You first create a thread that details what the method will be when run on the background thread. You then start the thread, passing in any parameters as needed. See MSDN for more info.