How to pass parameters into a thread?

R

rkbnair

How can I pass a parameter to a new thread?

Thread t = new Thread(new ThreadStart(ThreadProc));
t.Start();
Thread.Sleep(0);
 
M

Milosz Skalecki [MCAD]

Howdy,

You must use constructor accepting ParameterizedThreadStart delegate:

private void Whatever()
{
Thread thread = new Thread(new
ParameterizedThreadStart(this.ThreadProc));

int p1 = 10;
string p2 = "parameter2";
object[] parameters = new object[] { p1, p2 };

thread.Start(parameters);
}

private static void ThreadProc(object parameters)
{
object[] parameterArray = (object[]) parameters;

int p1 = (int)parameters[0];
string p2 = (string)parameters[1];
}
 
R

rkbnair

I'm getting an error on the line that says:

string p2 = (string)parameters[1];

Cannot apply indexing with [] to an expression of type 'object'

--
test


Milosz Skalecki said:
Howdy,

You must use constructor accepting ParameterizedThreadStart delegate:

private void Whatever()
{
Thread thread = new Thread(new
ParameterizedThreadStart(this.ThreadProc));

int p1 = 10;
string p2 = "parameter2";
object[] parameters = new object[] { p1, p2 };

thread.Start(parameters);
}

private static void ThreadProc(object parameters)
{
object[] parameterArray = (object[]) parameters;

int p1 = (int)parameters[0];
string p2 = (string)parameters[1];
}
--
Milosz


rkbnair said:
How can I pass a parameter to a new thread?

Thread t = new Thread(new ThreadStart(ThreadProc));
t.Start();
Thread.Sleep(0);
 
S

Scott Roberts

I believe the example should have been:

string p2 = (string)parameterArray[1];



rkbnair said:
I'm getting an error on the line that says:

string p2 = (string)parameters[1];

Cannot apply indexing with [] to an expression of type 'object'

--
test


Milosz Skalecki said:
Howdy,

You must use constructor accepting ParameterizedThreadStart delegate:

private void Whatever()
{
Thread thread = new Thread(new
ParameterizedThreadStart(this.ThreadProc));

int p1 = 10;
string p2 = "parameter2";
object[] parameters = new object[] { p1, p2 };

thread.Start(parameters);
}

private static void ThreadProc(object parameters)
{
object[] parameterArray = (object[]) parameters;

int p1 = (int)parameters[0];
string p2 = (string)parameters[1];
}
--
Milosz


rkbnair said:
How can I pass a parameter to a new thread?

Thread t = new Thread(new ThreadStart(ThreadProc));
t.Start();
Thread.Sleep(0);
 
M

Milosz Skalecki [MCAD]

Sorry, it should have been:

private static void ThreadProc(object parameters)
{
object[] parameterArray = (object[]) parameters;

int p1 = (int)parameterArray[0];
string p2 = (string)parameterArray[1];
}

--
Milosz


rkbnair said:
I'm getting an error on the line that says:

string p2 = (string)parameters[1];

Cannot apply indexing with [] to an expression of type 'object'

--
test


Milosz Skalecki said:
Howdy,

You must use constructor accepting ParameterizedThreadStart delegate:

private void Whatever()
{
Thread thread = new Thread(new
ParameterizedThreadStart(this.ThreadProc));

int p1 = 10;
string p2 = "parameter2";
object[] parameters = new object[] { p1, p2 };

thread.Start(parameters);
}

private static void ThreadProc(object parameters)
{
object[] parameterArray = (object[]) parameters;

int p1 = (int)parameters[0];
string p2 = (string)parameters[1];
}
--
Milosz


rkbnair said:
How can I pass a parameter to a new thread?

Thread t = new Thread(new ThreadStart(ThreadProc));
t.Start();
Thread.Sleep(0);
 
B

bruce barker

a better approach is to use an object:

class ThreadObject
{
public string Parameter1;
public int Status = 0;
private Thread t = null;
public Thread Start()
{
t = new Thread(this.work);
t.Start();
return t;
}
public Join () { t.Join(); }
private void work()
{
// do thread work
}
}

then to use it:

ThreadObject myThread = new ThreadObject ();
myThread.Parameter1 = "hello";
myThread.Start();
....
....
myThread.Join();
if (myThread.Status != 0) // do something



-- bruce (sqlwork.com)
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top