Communication between threads

  • Thread starter Thread starter Kosmas Nikolaidis
  • Start date Start date
K

Kosmas Nikolaidis

Hi,

I want to fill a datagrid from a separate thread.

How i use the invoke method?

thanks
 
Hello!

You can use invoke method from worker thread as

public delegate void GridFillInvoker(DataGrid dr);//Delegate


dataGrid.Invoke(new GridFillInvoker(FillGrid),new object[]{dr}); //Where
dr is the datarow...

It'll be easy to solve the problem if you tell exactly how do you want
to fill the grid.

Cheers :)
Maqsood Ahmed [MCP,C#]
Kolachi Advanced Technologies
http://www.kolachi.net
 
I did this before with help of some nice people like you out there..

here is my code..

DataSet myDataSet;
static private IAsyncResult ar;

// This function is called first, it then starts (Queues) a worker thread
// calling the function that will pull data from the DB server
public void UpdateNews(Int64 storyid)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(this.UpdateData), storyid);
}

// This the function the will fill DataSet from DB
private void UpdateData(Object state)
{

// fill dataset here
....
myDataAdapter.Fill(myDataSet);

try
{
MethodInvoker CallDataBindToDataGrid = new
MethodInvoker(UpdateGrid);
ar = this.BeginInvoke(CallDataBindToDataGrid);
}
catch
{
// Handle exceptions
}
}

// This is the function that actually updates the DataGrid
private void UpdateGrid()
{
dataGrid1.DataSource = myDataSet.Tables[0].DefaultView;

this.EndInvoke(ar);
}

I hope that will help

Saleh`
 
Hi Kosmas,

Kosmas Nikolaidis said:
Hi,

I want to fill a datagrid from a separate thread.

How i use the invoke method?

thanks

It seems you know that you cannot touch UI controls or any objects linked to
them, like a data source, from a thread that is not the UI thread that hosts
the UI control. Invoke allows you to synchronize access to the UI controls
or objects linked to them. An excerpt from the documentation on Invoke:

[Invoke] Executes the specified delegate on the thread that owns the
control's underlying window handle.

What Invoke does is post a delegate to the "event queue" of the UI thread
hosting the control. Invoke then blocks until the UI thread has executed the
delegate.

Back to your question. There are two strategies to filling the grid. (1)
Fill the grid in one shot, or (2) Fill the grid in chunks. The first is the
easiest way. I'll illustrate with two examples.

(1) The producer thread produces a DataTable, then Invoke's a method on the
DataGrid that replaces the grid's data source with the produced DataTable.
(2) The producer thread produces rows. When it has produced more than a
certain number of rows (for instance every 100 rows), it Invoke's a method
on the DataGrid that appends the produced rows to the data source of the
grid.

A good article on threading in .NET that might help you further is
http://www.yoda.arachsys.com/csharp/threads/

Hope this helps,
Tom T.
 
Hi Moqsood,

Maqsood Ahmed said:
Hello!

You can use invoke method from worker thread as

public delegate void GridFillInvoker(DataGrid dr);//Delegate

dataGrid.Invoke(new GridFillInvoker(FillGrid),new object[]{dr}); //Where
dr is the datarow...

This could become inefficient when there are a lot of rows, especially when
you use Invoke but also for BeginInvoke (asynchronous model). Invoke blocks
each time until the UI thread has finished execution of the delegate. So the
producer thread would block for each row until it is added to the grid :s.

Better to use chunks of rows.
It'll be easy to solve the problem if you tell exactly how do you want
to fill the grid.

Cheers :)
Maqsood Ahmed [MCP,C#]
Kolachi Advanced Technologies
http://www.kolachi.net

Kind regards,
Tom T.
 
Back
Top