Can't We pass Parameterized Method to a thread??
-
Hello All, I am trying to call a method through thread . it is working if the method does not contain any parameter, if it has got a parameter then it will pop up error stating that method name expected. Am i missing something here to perform this action. Thanks
Bharath.S Ron
-
Hello All, I am trying to call a method through thread . it is working if the method does not contain any parameter, if it has got a parameter then it will pop up error stating that method name expected. Am i missing something here to perform this action. Thanks
Bharath.S Ron
you need to use the ParameterizedThreadStart delegate which is part of the Threading namespace in .NET 2.0. i.e
public void RunThread() { int parameter = 20; Thread t = new Thread(new ParameterizedThreadStart(SampleMethodWithParameters)); t.Start(parameter); } private void SampleMethodWithParameters(object parameter) { for(int index = 0; index < Convert.ToInt32(parameter); index++) { Console.WriteLine("HelloWorld " + index.ToString()); Thread.Sleep(1000); } }
edit: no idea why my half my FOR loop is being displayed but hopefully that should give you an idea. -- modified at 3:28 Wednesday 25th July, 2007
-
you need to use the ParameterizedThreadStart delegate which is part of the Threading namespace in .NET 2.0. i.e
public void RunThread() { int parameter = 20; Thread t = new Thread(new ParameterizedThreadStart(SampleMethodWithParameters)); t.Start(parameter); } private void SampleMethodWithParameters(object parameter) { for(int index = 0; index < Convert.ToInt32(parameter); index++) { Console.WriteLine("HelloWorld " + index.ToString()); Thread.Sleep(1000); } }
edit: no idea why my half my FOR loop is being displayed but hopefully that should give you an idea. -- modified at 3:28 Wednesday 25th July, 2007