0

I'm currently in the process of creating a class that has general methods that specialised methods of the class call. One of these general methods needs to get a method as parameter to use it to start a new thread.

My question is how this parameter must be done (thus what I must replace with, or what do I need to change in total to make it work as intended):

Class mytest
{
   public void myPublicStarter()
   {
       Starter("Test", this.DoWork);
   }

   public void DoWork(object myIDString)
   {

   }

   private void Starter(string myIDString, <METHOD> paramethod)
   {
        Thread myThread = new Thread(paramethod);
        myThread.Start(myIDString);
   }

}
Thomas
  • 2,886
  • 3
  • 34
  • 78
  • possible duplicate of [How to pass parameters to ThreadStart method in Thread?](http://stackoverflow.com/questions/3360555/how-to-pass-parameters-to-threadstart-method-in-thread) – Yuval Itzchakov May 30 '14 at 08:02
  • Duplicate: http://stackoverflow.com/questions/3360555/how-to-pass-parameters-to-threadstart-method-in-thread – Yuval Itzchakov May 30 '14 at 08:03
  • 1
    @YuvalItzchakov it's not a dup. he's looking for the parameter type. – Royi Namir May 30 '14 at 08:03
  • 2
    have a look: (http://stackoverflow.com/questions/2082615/pass-method-as-parameter-using-c-sharp) – Stone May 30 '14 at 08:03
  • @YuvalItzchakov, I don't think that this is a duplicate. It's not asking how to pass parameters to a thread entry method but rather how to pass the thread entry method itself. – jmcilhinney May 30 '14 at 08:04
  • Exactly. I saw http://stackoverflow.com/questions/3360555/how-to-pass-parameters-to-threadstart-method-in-thread before I posted this thread but it didnt answer how to pass the method itself to another method to THERE then start the thread. That is why I made this question. – Thomas May 30 '14 at 08:09
  • @ThomasE. Stone comment have the answer. – Bharadwaj May 30 '14 at 08:12
  • not completely. As there is Func used in that one questions answers there. And Func needs a return parameter (which I don't have). The answers here with Action or ParameterizedThreadStart work with no return value (Func on the other hand needs one). – Thomas May 30 '14 at 08:15

2 Answers2

2

When you create a new Thread object you have to pass it a delegate, i.e. an object that refers to a method. That delegate must be type ThreadStart if the entry method has no parameters and ParameterizedThreadStart if it does. In your case, your DoWork method has a parameter so the delegate type must be the latter. Because the paramethod parameter is declared as that type, the compiler knows to cast this.DoWork as that type when you call Starter. If the method signature did not match that of the delegate then a compilation error would occur.

public void myPublicStarter()
{
    Starter("Test", this.DoWork);
}

public void DoWork(object myIDString)
{

}

private void Starter(string myIDString, ParameterizedThreadStart paramethod)
{
     Thread myThread = new Thread(paramethod);
     myThread.Start(myIDString);
}
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
1

Well, in .NET you have the concept of delegates, and you have Actions and Functions which are in fact just delegates.

A Function represents a function that takes some input parameters and has a return value. An action represents a method that takes some input parameters but doesn't return any value.

So it's depending on what you woud like to achieve.

Here's a demo sample:

static void Main(string[] args)
{
    myPublicStarter();
}

public static void myPublicStarter()
{
    Starter("Test", () => DoWork("My Id String Passed here"));
}

public static void DoWork(object myIDString)
{
    Console.WriteLine(myIDString);
}

private static void Starter(string myIDString, Action paramethod)
{
    paramethod.Invoke();
}

}

Note: I've made the classes static as I've converted it to a console application.

That's the output.

Now, to adapt the code to work with threads, you can use the following:

static void Main(string[] args)
{
    myPublicStarter();
}

public static void myPublicStarter()
{
    Starter("Test", x => DoWork("My Id String Passed here"));
}

public static void DoWork(object myIDString)
{
    Console.WriteLine(myIDString);
}

private static void Starter(string myIDString, Action<object> paramethod)
{
    Thread myThread = new Thread(x => paramethod(x));
    myThread.Start(myIDString);
}

This will generate the exact same output as listed above.

Complexity
  • 5,682
  • 6
  • 41
  • 84