Passing data to Thread

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

How I can pass data to thread or how I can access data from the thread?

I want to start thread that can modify some data (datarow or dataset for
example)

any ideas?
 
You can't pass data to an existing thread like you would a method. To get
data to a thread there are two classic mechanisms. You can pass all the
data as part of the start up of the thread. Or you can store your data in
an area accessible to the thread. The thread would then grab it as needed.
 
Andrew,

Can you give me sample?

i did check the start thread and dont accept arguments,

I prefer to share data to the thread, or pass data byref,

thanks
Enrique Vizcarra

Andrew Faust said:
You can't pass data to an existing thread like you would a method. To get
data to a thread there are two classic mechanisms. You can pass all the
data as part of the start up of the thread. Or you can store your data in
an area accessible to the thread. The thread would then grab it as needed.


--
Andrew Faust
andrew[at]andrewfaust.com
http://www.andrewfaust.com


SISTEC said:
Hi,

How I can pass data to thread or how I can access data from the thread?

I want to start thread that can modify some data (datarow or dataset for
example)

any ideas?
 
There is an overloaded constructor and Start on the Thread class that let's
you pass in an object parameter. This object parameter can be whatever you
want it to be, including an array containing other data. The MSDN talks
about it:

http://msdn2.microsoft.com/en-us/library/6x4c42hc.aspx
I prefer to share data to the thread,

The thread has access to all the public, private & protected data elements
the that the code you're running would have even if it weren't in a
separate thread. For example:

class TestClass
{
protected string m_TestString = "Hello World";

public LaunchThread()
{
Thread th = new Thread(new ParameterizedThreadStart(DoStuff));
th.Start("This is the value to add while in the thread");
}

private void DoStuff(object NewValue)
{
m_TestString = "This is an updated value from a thread: " +
NewValue.ToString();
}
}

Just beware that if multiple threads are updating the same shared variables
you can have some strange issues show up. I suggest you read up (preferably
a book, not just Google) on concurrent program to learn about locks, race
conditions, deadlocks, etc.

--
Andrew Faust
andrew[at]andrewfaust.com
http://www.andrewfaust.com


SISTEC said:
Andrew,

Can you give me sample?

i did check the start thread and dont accept arguments,

I prefer to share data to the thread, or pass data byref,

thanks
Enrique Vizcarra

Andrew Faust said:
You can't pass data to an existing thread like you would a method. To
get
data to a thread there are two classic mechanisms. You can pass all the
data as part of the start up of the thread. Or you can store your data
in
an area accessible to the thread. The thread would then grab it as
needed.


--
Andrew Faust
andrew[at]andrewfaust.com
http://www.andrewfaust.com


SISTEC said:
Hi,

How I can pass data to thread or how I can access data from the
thread?

I want to start thread that can modify some data (datarow or dataset
for
example)

any ideas?
 
Thank you very much Andrew

Now it's working fine :)

you sample code was helpfully



Enrique Vizcarra

Andrew Faust said:
There is an overloaded constructor and Start on the Thread class that let's
you pass in an object parameter. This object parameter can be whatever you
want it to be, including an array containing other data. The MSDN talks
about it:

http://msdn2.microsoft.com/en-us/library/6x4c42hc.aspx
I prefer to share data to the thread,

The thread has access to all the public, private & protected data elements
the that the code you're running would have even if it weren't in a
separate thread. For example:

class TestClass
{
protected string m_TestString = "Hello World";

public LaunchThread()
{
Thread th = new Thread(new ParameterizedThreadStart(DoStuff));
th.Start("This is the value to add while in the thread");
}

private void DoStuff(object NewValue)
{
m_TestString = "This is an updated value from a thread: " +
NewValue.ToString();
}
}

Just beware that if multiple threads are updating the same shared variables
you can have some strange issues show up. I suggest you read up (preferably
a book, not just Google) on concurrent program to learn about locks, race
conditions, deadlocks, etc.

--
Andrew Faust
andrew[at]andrewfaust.com
http://www.andrewfaust.com


SISTEC said:
Andrew,

Can you give me sample?

i did check the start thread and dont accept arguments,

I prefer to share data to the thread, or pass data byref,

thanks
Enrique Vizcarra

Andrew Faust said:
You can't pass data to an existing thread like you would a method. To
get
data to a thread there are two classic mechanisms. You can pass all the
data as part of the start up of the thread. Or you can store your data
in
an area accessible to the thread. The thread would then grab it as
needed.


--
Andrew Faust
andrew[at]andrewfaust.com
http://www.andrewfaust.com


Hi,

How I can pass data to thread or how I can access data from the
thread?

I want to start thread that can modify some data (datarow or dataset
for
example)

any ideas?
 
Back
Top