Reputation: 1202
I just created function of thread:
static void ThreadMethod {}
And try this:
ThreadPool.QueueUserWorkItem(ThreadMethod);
But QueueUserWorkItem
requests WaitCallback
object.
It's just example from my book and looks like it must work this way. What am i missing?
Upvotes: 1
Views: 89
Reputation: 13059
I think it's a typo
Not :
static void ThreadMethod {}
But :
static void ThreadMethod(object sender) {//Method stuff go here}
And call it as,
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadMethod));
A working minimal case + parameter passing to thread
using System;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
paras myvalues = new paras();
myvalues.para1 = 10;
myvalues.para2 = 20;
ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadMethod),myvalues);
Console.ReadKey();
}
static void ThreadMethod(object state)
{
paras vals =(paras) state;
Console.WriteLine(vals.para1);
Console.WriteLine(vals.para2);
}
}
struct paras
{
int Para1;
public int para1
{
get { return Para1; }
set { Para1 = value; }
}
int Para2;
public int para2
{
get { return Para2; }
set { Para2 = value; }
}
}
}
Upvotes: 3