passing parameters to a thread function

  • Thread starter Thread starter Carlos
  • Start date Start date
C

Carlos

Hi all,

I am familiar with passing parameters to a thread function using C++,
but I needt to learn it using C#. Can someone shed some light on
how to do this? Code snippets will be great to show me.

Thanks in advance,

Carlos
 
Carlos said:
I am familiar with passing parameters to a thread function using C++,
but I needt to learn it using C#. Can someone shed some light on
how to do this? Code snippets will be great to show me.

class ThreadTest
{
public int i;
public void Threadproc()
{
// use i
}
}

Thread t = new Thread(new ThreadStart(test.Threadproc));
t.Start();

A dirty way to pass parameters to a thread is via the Thread.Name
property, which works, but is not elegant:

Thread t = new Thread(new ThreadStart(Threadproc));
int i = 42;
t.Name = i.ToString();
t.Start();

void Threadproc()
{
int i = Int32.Parse(Thread.CurrentThread.Name);
// use i
}

Of course, if you use a ThreadPool thread, you can pass a parameter.

..NET v2 will allow you to pass a parameter to a thread.

Richard
 
Back
Top