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.

ssilas777
  • 9,672
  • 4
  • 45
  • 68
elisa
  • 743
  • 2
  • 13
  • 31
  • 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? – Doug S. Mar 01 '11 at 14:44
  • yes I have an error: cannot convert from method group to System.Threading.ParameterizedThreadStart – elisa Mar 01 '11 at 14:46

5 Answers5

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.

Justin
  • 6,611
  • 3
  • 36
  • 57
  • 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 Mar 01 '11 at 15:01
  • @elisa - No, you need to create a new thread for each call to `Start`. Per [the documentation of Thread.Start](http://msdn.microsoft.com/en-us/library/a9fyxz7d.aspx): "Once the thread terminates, it cannot be restarted with another call to Start." – Justin Mar 01 '11 at 15:10
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

Community
  • 1
  • 1
Peter Kelly
  • 14,253
  • 6
  • 54
  • 63
  • I have the error: no overload for "Method"" matches delegate "System.Threading.WaitCallback". Please help – elisa Mar 01 '11 at 14:52
  • WaiCallback != WaitCallback. Double-check your spelling. – user Mar 01 '11 at 14:53
  • OK...sorry for mistake. But the error still persist. Any ideas for how to solve it? – elisa Mar 01 '11 at 14:57
  • 1
    @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. – Peter Kelly Mar 01 '11 at 14:59
  • @elisa Please try again - you do not need to change your method signature to an object with this one. – Peter Kelly Mar 01 '11 at 15:10
  • @Peter I realize this is an old question, but would you be able to explain what the Lambda expression is actually doing here? – Rob Aug 01 '16 at 16:54
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) {.....}
S M Kamran
  • 4,423
  • 7
  • 25
  • 35
  • 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) – Justin Mar 02 '11 at 19:27
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.

Tim Rogers
  • 21,297
  • 6
  • 52
  • 68
-1

please try:

Thread t = new Thread(new ThreadStart(method)); 
t.Start();
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • 1
    I have an error using your method: No overload for Method matches System.Threading.ThreadStart. – elisa Mar 01 '11 at 14:47
  • In place of ThreadStart(), ParameterizedThreadStart() should be used for parameterised methods, as per the problem mentioned in the question. – Ambuj Jan 04 '17 at 20:34