threading parameter

  • Thread starter Thread starter grmAbay
  • Start date Start date
G

grmAbay

Hello,

This might sound as a stupid question, but how can I pass parameters to a
thread? Let me rephrase that.

When I have a method, i declare it like this:

public void DoSomething(string aParameter, int anotherParameter) {
....
}

When I want to use this method with a thread I do it like this:
Thread doSomethingThread= new Thread(new ThreadStart(this.DoSomething));

doSomethingThread.Start();



Where do I add the parameters?
 
Hi,
In addition to what you can find in Jon's site I want to give you one more
idea.
You can decalre a delegate for the method you want to start in a thread.

//Delegate and the method itslef might have a return value as well
delegate void ThreadMethod(string aParameter, int anotherParameter) ;

public void DoSomething(string aParameter, int anotherParameter) {
....
}

ThreadMethod starter = new ThreadMethod(DoSomething);

Now you can start DoSomething in a separate thread:

starter.BeginInvoke(param1, param2, null, null);

You can use IAsyncResult returned by BeginInvoke and use EndInvoke to wait
for the thread to finish and get result. You can optain a wait handle,
checking periodically IAsyncResult.IsCompleted or using callback method. You
have options.

Begin invoke uses a thread form the thread pool

More info you can find in MSDN
ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpovrasynchronousprogrammingo
verview.htm

HTH
B\rgds
100
 
Back
Top