Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No overload for '' matches delegate 'System.Threading.ParameterizedThreadStart'

If i have a method like this

  private void LoadModel(List<object> filenames)
{
}

and want to run this method in thread i make this

loadingThread = new ParameterizedThreadStart(LoadModel)

but give me error

how to solve this problem ?

No overload for 'LoadModel' matches delegate 'System.Threading.ParameterizedThreadStart'    
like image 959
Ahmed Avatar asked Oct 14 '15 22:10

Ahmed


2 Answers

Another way to solve your problem that allows your method to still have the type of the parameter that you want is to use a lambda expression like this:

Thread thread = new Thread(() => LoadModel(list));

Where list is the parameter value that you want to pass.

like image 22
Yacoub Massad Avatar answered Nov 03 '22 08:11

Yacoub Massad


This delegate is defined as

public delegate void ParameterizedThreadStart(object obj)

You have to change your method declaration to match it:

private void LoadModel(object filenames)

and cast filenames to List in the method.

To create and start the thread use

Thread loadingThread = new Thread(LoadModel);
loadingThread.Start(filenames);

Instead of creating your own threads, consider using Tasks or ThreadPool.

like image 175
Jakub Lortz Avatar answered Nov 03 '22 09:11

Jakub Lortz