Threading

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hi,all

this is a stupid question.

I want to use thread
when I create a new ThreadStart,

ThreadStart thread = new System.ThreadThreading.ThreadStart(ThreadProc)

I want to pass some parameter to ThreadProc
how to do it?

thanks
 
if all you need is to share some data with ThreadProc, you should declare a
class variable that will be assigned the data. (watch out for
synchronisation issues if you are going to be modifying the value in
question).

if you absolutely want to pass some parameters, you should declare a
delegate for the method you want to call and use the BeginInvoke method.
Like this:


public class example
{
private void mythreadmethod(string someparam)
{
//do whatever
}

public delegate void ParamMethod(string someParam);

public static void Main()
{
example ex = new example();
ParamMethod pm = new ParamMethod(ex.mythreadmethod);
string argToPass = "hello";
pm.BeginInvoke(argToPass, null, null);
}
}

This is a very simple example (for example, a lot can be said about the last
2 parameters of the BeginInvoke method ...) but it illustrates what you need

HTH
Cois
 
Back
Top