Threading Issue

M

Mythran

Part #1:
I have a Thread, MainThread, and a child thread that is started in the
MainThread called ChildThread.

I have a DataSet created in the MainThread in which needs to be passed to
the ChildThread. The ChildThread needs to update the DataSet with new data.
The MainThread, after the ChildThread is finished, needs to continue on it's
merry way updating the DataSet.


Part #2:
I have MainThread and ChildThread ... in ChildThread, I need to create a
DataSet and/or a DataTable. The DataSet then needs to be passed to the
MainThread and then bound to either a DataGrid or Crystal Report...

I have tried both of the above questions but seem to get the same result
every time. Something about can't reference an object created on a
different thread. It was some time ago, and I just remembered about it and
am now curious. I can try setting up a project and duplicating my problems
again if need be...but hopefully there is a short/semi-short method that can
be described using pseudo/semi-pseudo code...

The problem is for C# or VB.Net, either will solve for the other...as it
should be, if any examples/code is given.

Thanks!

Mythran
 
D

Dave

Mythran said:
Part #1:
I have a Thread, MainThread, and a child thread that is started in the
MainThread called ChildThread.

I have a DataSet created in the MainThread in which needs to be passed to
the ChildThread. The ChildThread needs to update the DataSet with new
data. The MainThread, after the ChildThread is finished, needs to continue
on it's merry way updating the DataSet.

Take a look at this...not a difficult job i think:

///////////////////////////////////////////////////////////
class Application
{
void MainJob()
{
Thread.CurrentThread.Name = "Main thread";
ds = new DataSet();
ds.Create();
Thread th = new Thread(new ThreadStart(this.ChildJob));
th.Name = "Child thread";
th.Start();
th.Join();
ds.Update();
//....
}
void ChildJob()
{
ds.Update();
}
DataSet ds; // a shared dataset
}
}

public class DataSet
{
public DataSet()
{
Console.WriteLine("Dateset instantiated on thread: {0}",
Thread.CurrentThread.Name);
}
public void Create()
{
Console.WriteLine("DataSet created on thread: {0}" ,
Thread.CurrentThread.Name);
}
public void Update()
{
Console.WriteLine("DataSet updated on thread: {0}",
Thread.CurrentThread.Name);
}
}

///////////////////////////////////////////////
If you are afraid of multiple access to your dataset by many thread, simply
use the lock(this) statement (Monitor.Enter(....) and Monitor.Exit() in VB)
 

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

Top