It works on Windows, Unity (Mono, Android, iOS, Windows Phone)
The simplest case first: Executing a method disregarding the argument
private void SimpleThreadPoolTest() { ThreadPool.QueueUserWorkItem(new WaitCallback(SimpleMethod)); } private void SimpleMethod(object ignoredArg) { Console.WriteLine("Method called inside a thread"); }
As you may have noticed, the WaitCallback delegate takes one argument of type object, which we're ignoring right now.
But if you need to use, just pass any object as the second argument of QueueUserWorkItem, like:
private void ParameterizedThreadPoolTest() { string arg = "This is a string parameter"; ThreadPool.QueueUserWorkItem(new WaitCallback(SimpleMethod), arg); } private void WaitCallback(object arg) { Console.WriteLine("Executing inside a thread with argument "+arg); }
You can also use lambda expressions instead of methods
private void LambdaThreadPoolTest() { // unusedArgument is required, even though we're not using it, because WaitCallback takes 1 argument ThreadPool.QueueUserWorkItem(new WaitCallback( (unusedArgument)=> { // do something // then do something else Console.WriteLine("This is a lambda function running inside a thread"); } )); }