0

Possible Duplicate:
C# ThreadStart with parameters

How to put a method with one parameter into the Thread C#.

Example:

Thread thread = new Thread(SoundInputThread.getSomething(hz));
                 thread.Start();
                 for (int i = 0; i < 5; i++) {
                     Console.WriteLine();
                     Thread.Sleep(1000);
                 }


     public static void getSomething(int hz) {
            hz = 100;
            Console.WriteLine();
        }
Community
  • 1
  • 1
Robert
  • 233
  • 3
  • 12

2 Answers2

3

You can capture the value as follows:

Thread thread = new Thread(() => {
   getSomething(hz);
});
thread.Start();
Candide
  • 30,469
  • 8
  • 53
  • 60
  • The primary advantage of this over a parameterized thread start is that you don't need to cast (or box) the parameter, and also that you can have more than one parameter. – Servy Nov 14 '12 at 16:42
1

You need to use overloaded constuctor of Thread which takes ParameterizedThreadStart It will allow passing parameter to thread method. In the method you can csat object back to your type.

thread = new Thread(new ParameterizedThreadStart(getSomething));
thread.Start(2);

public static void getSomething(object obj) {
      int i = (int)obj;
}
Adil
  • 146,340
  • 25
  • 209
  • 204