How would i pass some parameters to a new thread that runs a function from another class ?
What i’m trying to do is to pass an array or multiple variables to a function that sits in another class and its called by a new thread.
i have tried to do it like this >
Functions functions = new Functions();
string[] data;
Thread th = new Thread(new ParameterizedThreadStart(functions.Post()));
th.Start(data);
but it shows error “No overload for method ‘Post’ takes 0 arguments”
Any ideas ?
Since you have this flagged C# 4, the new approach to this would be:
If you really want to leave this using a dedicated thread, and not the Task Parallel library, you can. Given your comments, it sounds like
Post()is probably defined asPost(string[] data). This will not work sinceParameterizedThreadStartexpects the method to bePost(object data).You can work around this via lambdas and using ThreadStart instead of ParameterizedThreadStart, however, without changing your methods: