Mayank
Mayank

Reputation: 29

Threading error in C#

Ok so i am using this line of code in project

System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(ProcessReport));

but it is throwing the following error

No overload for the "ProcessReport" matches delegate “System.Threading.WaitCallback

I replace the above mentioned line with the following lines:

Thread t = new Thread(new ThreadStart(ProcessReport));
t.Start();

This removed the error but is this the right way to do it. I cannot check the output right there as i am an intern in a company and this is part of the whole big project. Please help.

I cannot post the whole ProcessReport as i am not allowed to but it starts with this :

public void ProcessReport()

Upvotes: 1

Views: 2202

Answers (2)

Chibueze Opata
Chibueze Opata

Reputation: 10044

One of the possible problems that could occur with

Thread t = new Thread(new ThreadStart(ProcessReport));

t.Start();

is that if you start so many threads in that way without checking for any conditions, your program is likely to crash or even the system in some cases. However if it's an app that an event has to occur before this thread is started, your code should be fine.

Upvotes: 0

vcsjones
vcsjones

Reputation: 141678

My assumption is that since this compiles:

new ThreadStart(ProcessReport)

Your ProcessReport method looks something like this:

void ProcessReport()
{
}

QueueUserWorkItem takes a WaitCallback delegate, which requires passing a single object as the parameter. So change your method's signature to look like this:

void ProcessReport(object state)
{
}

And you should be OK. The state parameter you can ignore if you don't need to use it, but it's value is whatever you pass in as a second parameter of QueueUserWorkItem. Since you are using the overload that doesn't pass an object into QueueUserWorkItem, it will always be null.

Upvotes: 6

Related Questions