How to pass parameters into a thread?
am 29.01.2008 22:46:01 von rkbnairHow can I pass a parameter to a new thread?
Thread t = new Thread(new ThreadStart(ThreadProc));
t.Start();
Thread.Sleep(0);
How can I pass a parameter to a new thread?
Thread t = new Thread(new ThreadStart(ThreadProc));
t.Start();
Thread.Sleep(0);
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);
>
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);
> >
I believe the example should have been:
string p2 = (string)parameterArray[1];
"rkbnair"
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);
>> >
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);
> > >
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);
>