C# Thread parameter question

S

Serge Klokov

Hi!

When I create a Thread

Thread t1 = new Thread( new ThreadStart(Incrementer) );

How I can pass param in function "Increment"?

if i use

Thread t1 = new Thread( new ThreadStart(Incrementer(12)) );

i'm getting an error.

public void Incrementer( int myParam )
{
//do something...
}


Thank you,
Serge
 
S

Sharon

Hi Serge.
You can instantiate the thread class and store the object that you need to
pass
in a member of this class.
You can do that in the constructor, method set or property set.
Then you can use this member in the thread start method.
Sharon.

public class ThreadProc
{
private string m_str;

public ThreadProc(string p_str)
{
m_str = p_str;
}

public void ThreadProcStart()
{
// do something with m_str
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top