18

If I have a

public void Method(int m) { ... } 

how can I create a thread to this method?

Thread t = new Thread((Method));

t.Start(m);

is not working.

2
  • 1
    What exactly is happening? What isn't working? Are you getting an error message or is the method just not working? Can you post some additional code? Commented Mar 1, 2011 at 14:44
  • yes I have an error: cannot convert from method group to System.Threading.ParameterizedThreadStart Commented Mar 1, 2011 at 14:46

5 Answers 5

29

You can do this using a lambda expression. The C# compiler automatically creates the ThreadStart delegate behind the scenes.

Thread t = new Thread(() => Method(m)); t.Start(); 

Note that if you change m later in your code, the changes will propagate into the thread if it hasn't entered Method yet. If this is a problem, you should make a copy of m.

Sign up to request clarification or add additional context in comments.

2 Comments

It works. Thx a lot. Justin I would like to ask you if I use t.Start it means that every time i want to call this method I will be able to run the code?
@elisa - No, you need to create a new thread for each call to Start. Per the documentation of Thread.Start: "Once the thread terminates, it cannot be restarted with another call to Start."
14

The method you are calling requires a parameter. Because it has one parameter and a return type of void you can use the following

ThreadPool.QueueUserWorkItem(o => Method(m)); 

You do not need to change the int to an object in the method signature using this method.

There are advantages to using the ThreadPool over starting your own Thread manually. Thread vs ThreadPool

6 Comments

I have the error: no overload for "Method"" matches delegate "System.Threading.WaitCallback". Please help
WaiCallback != WaitCallback. Double-check your spelling.
OK...sorry for mistake. But the error still persist. Any ideas for how to solve it?
@elisa You need to change the signature of the method to Method(object m) and then cast to an int in the body of the method.
@elisa Please try again - you do not need to change your method signature to an object with this one.
|
9
ThreadStart tsd = new ThreadStart(ThreadMethod); Thread t = new Thread(tsd); t.Start(); 

Thread methods needs to be a method with return type void and accepting no argument.

public void ThreadMethod() {.....} 

There is another variant which is ParameterizedThreadStart

ParameterizedThreadStart ptsd = new ParameterizedThreadStart(ThreadParamMethod); Thread t = new Thread(ptsd); t.Start(yourIntegerValue); 

ThreadParamMethod is a method which return type is void and accept one argument of type object. However you can pass just about any thing as object.

public void ThreadParamMethod(object arg) {.....} 

1 Comment

Can't you shorten the ParameterizedThreadStart example by getting rid of ptsd and just doing Thread t = new Thread(ThreadParamMethod);? Will the compiler automatically construct the ParameterizedThreadStart delegate? (I honestly haven't tried)
2

Method needs to take an object not an int to be able to use the ParameterizedThreadStart delegate.

So change m to an object and cast it to an int first off.

Comments

-1

please try:

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

2 Comments

I have an error using your method: No overload for Method matches System.Threading.ThreadStart.
In place of ThreadStart(), ParameterizedThreadStart() should be used for parameterised methods, as per the problem mentioned in the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.