Sam
Sam

Reputation: 3

C#: No overload for 'function' matches delegate System.Threading.WaitCallBack

So I've been using threads lately and the error (C#: No overload for 'Generic_Function' matches delegate System.Threading.WaitCallBack) is confusing me a bit.

At first I thought there had to be a parameter passed, then I learned it could easily be null.

ThreadPool.QueueUserWorkItem(new WaitCallback(Generic_Function), null);

Shouldn't be the function then defined as:

void Generic_Function(){
 //Code here
}

However, I'm getting an error. I'm missing something here and I'm boggled. I've tried reading the docs but I guess I'm not grasping this fully. I'd appreciate if someone could explain. Much thanks!

Upvotes: 0

Views: 369

Answers (2)

Aleks Andreev
Aleks Andreev

Reputation: 7054

If for some reasons you can't change Generic_Function signature you can wrap a call with lambda:

ThreadPool.QueueUserWorkItem(_ => Generic_Function());

Upvotes: 0

John Wu
John Wu

Reputation: 52240

According to the documentation, a WaitCallback is defined like this:

public delegate void WaitCallback(object state);

So you can make this modification if you'd like your code to work:

void Generic_Function(object state)
{
    //Code here
}

Upvotes: 1

Related Questions