How to pass parameters into a thread?

How to pass parameters into a thread?

am 29.01.2008 22:46:01 von rkbnair

How can I pass a parameter to a new thread?

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

RE: How to pass parameters into a thread?

am 29.01.2008 23:01:01 von mily242

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" wrote:

>
> How can I pass a parameter to a new thread?
>
> Thread t = new Thread(new ThreadStart(ThreadProc));
> t.Start();
> Thread.Sleep(0);
>

RE: How to pass parameters into a thread?

am 29.01.2008 23:32:01 von 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 [MCAD]" wrote:

> 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" wrote:
>
> >
> > How can I pass a parameter to a new thread?
> >
> > Thread t = new Thread(new ThreadStart(ThreadProc));
> > t.Start();
> > Thread.Sleep(0);
> >

Re: How to pass parameters into a thread?

am 29.01.2008 23:41:38 von Scott Roberts

I believe the example should have been:

string p2 = (string)parameterArray[1];



"rkbnair" wrote in message
news:47969C81-9412-42EB-A9C3-FEA3593DA7A1@microsoft.com...
> 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 [MCAD]" wrote:
>
>> 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" wrote:
>>
>> >
>> > How can I pass a parameter to a new thread?
>> >
>> > Thread t = new Thread(new ThreadStart(ThreadProc));
>> > t.Start();
>> > Thread.Sleep(0);
>> >

RE: How to pass parameters into a thread?

am 30.01.2008 00:17:13 von mily242

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" wrote:

> 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 [MCAD]" wrote:
>
> > 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" wrote:
> >
> > >
> > > How can I pass a parameter to a new thread?
> > >
> > > Thread t = new Thread(new ThreadStart(ThreadProc));
> > > t.Start();
> > > Thread.Sleep(0);
> > >

RE: How to pass parameters into a thread?

am 30.01.2008 01:33:28 von brucebarker

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)


"rkbnair" wrote:

>
> How can I pass a parameter to a new thread?
>
> Thread t = new Thread(new ThreadStart(ThreadProc));
> t.Start();
> Thread.Sleep(0);
>