How can i pass data to a thread in .net?

  • Thread starter Thread starter Faraz Rasheed
  • Start date Start date
F

Faraz Rasheed

How can I pass some data to a thread. I tried GetData() and SetData()
methods of Thread class but couldn’t solve the problem. Consider the
following code

public void SomeMethod(int somevalue)
{
Thread t = new Thread(thFunc);
// make variable ‘somevalue’ accessible to the thread t
t.Start();
return;
}

public void thFunc()
{
int valuehere;
// retrieve the value of variable ‘somevalue’ and store it to vaiable
‘valuehere’
// the following line should print the value of variable ‘somevalue’
Console.WriteLine(valuhere);
}
 
Faraz Rasheed said:
How can I pass some data to a thread. I tried GetData() and SetData()
methods of Thread class but couldn't solve the problem. Consider the
following code

this is really simple. you just have to create a wrapper class.

class MyThread
{
int a, b, c, d...
string aa,bb
// any other variables you need in thread

public MyThread( _parameter_list_)
{
// initialize parameters
}

// body of the thread
public void thFunc()
{
// use parameters as you want them
}
}

MyThread myThread = new MyThread( parameter_list );
// make use of the inner method of myThread that can access your parameters
Thread t = new Thread(myThread.thFunc); object
 
Well this is not what i want. I know this but the problem is that i cant
create a new class for the thread as i have to use the current logic and
code written in the class. All the threads will be using the members of
theis class. The situation is like

class LegacyClass : SomeBaseLegacy
{
private int somenum;
// ... and so on

// code in the first message goes here

}
 
You will need to place a "critical section" around code that accesses
'somevalue'. The easiest way is with "lock(someObject)" or by using
functions of the Monitor class.

This is a really basic threading question you are asking. Rather than
answering it directly, I urge you to research threading. You shouldn't
touch it until you feel comfortable you understand it. Bugs in threaded
code are a good way to cause very sneaky bugs, and you, my friend, know just
enough to be dangerous! I mean that in a good way, of course!
 
Jason,
If you use the approach below you will be able to pass a value to a new
thread.
In case you need communication between 2 threads then you can create a
GetValue/SetValue protected with critical section in the
ThreadInitializationObject. Please check the code and let me know if it
works for you.
Thanks.

//---------------------------- BEGIN
ODE -------------------------------------
using System;
using System.Threading;

namespace TestPassingValueToThread
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
ThreadInitializationObject initialObject = new
ThreadInitializationObject("VALUE PASSED FROM THREAD 1");

Thread t = new Thread( new ThreadStart(initialObject.Run) );
t.Start();
return;
}
}

public class ThreadInitializationObject
{
public readonly string m_StringValue;
public ThreadInitializationObject(string InitialString)
{
m_StringValue = InitialString;
}

public void Run()
{
Console.WriteLine("Value inside new thread = '{0}'", m_StringValue);
}
}
}

//---------------------------- END
ODE -------------------------------------

--
Thanks.
Paulo

DISCLAIMER: This posting is provided "AS IS" with no warranties, and confers
no rights. Use of any included code samples are subject to the terms
specified at http://www.microsoft.com/info/cpyright.htm"
 
Back
Top