Listen event on the different thread and update data on the UI thread

R

Rajat

Hi,



Sorry for the long class. This time I am sending you much cleaner code.

Could you please respond me back, about "how to apply the threading", taking
the example of following scenario .



namespace ThreadingTest

{



public class Class1

{

public event EventHandler raiseResponse; //Event through which I can send
the received data



DataFeeder instDataFeeder = new DataFeeder(); //Object created
for the datafeeder component.



//Constructor

public Class1

{

//CustomEventHandler is the delegate to handle ResponseData event from the
DataFeeder component

instDataFeeder.ResponseData += new
CustomEventHandler(InstDataFeeder_ResponseData);

}



public void RequestData()

{

instDataFeeder.RequestDataFromRemoteServer();

}



public void InstDataFeeder_ResponseData(NewDataStructure newData) //
NewDataStructure contains multiple rows of data at the same time

{

//we receive this event really really really fast and I was
thinking of putting this event on the separate thread.

//Fill Datatable viz. dataTable1 from newData and then raise
event

If(raiseResponse != null)

raiseResponse(dataTable1,EventArgs.Empty); //
Sending the datatable in sender to avoid creating a custom class for event
argument.

}

}



//In the following usercontrol we need to bind the data received from our
own event -- raiseResponse

public class GridControl : System.Windows.Forms.UserControl

{



private DataGrid dataGrid1 = new DataGrid();

Class1 instClass1 = new Class1();

DataTable dt = new DataTable() ; //It has some of the same
columns received from the other class, Not creating the full structure for
the table.



//Constructor

public GridControl ()

{

instClass1. raiseResponse += new
EventHandler(instClass1_raiseResponse);

dataGrid1.DataSource = dt;



}



public void instClass1_raiseResponse(object sender,EventArgs e)

{

//If the data is coming up very fast then there will be problem
updating the UI and UI will freeze.



dataGrid1.SuspendLayout()

DataTable dt = (DataTable) sender; // I think assigning to the
datatable will update the datagrid.

dataGrid1.ResumeLayout()

}

}

}



Please help.



Regards,

Rajat.
 
C

Colin Neller

Your question is very vague, but I think I figured what you're asking.
See if the following code answers your question:

using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System;

namespace ThreadingTest
{
public class Class1
{
public event EventHandler raiseResponse; //Rajat - Event
through which I can send the received data

private object _myObject = new object(); //Rajat - Object
created for the datafeeder component.
private Control _syncControl; //neller - a reference to the
container control

//Rajat - Constructor
public Class1(Control syncControl)
{
//neller - save off the container control
_syncControl = syncControl;
}

public void myObject_ResponseData(object newData) //
NewDataStructure contains multiple rows of data at the same time
{
DataTable dt = new DataTable(); //neller - create table

//TODO: neller - fill dt with new data

if (raiseResponse != null)
{
if (_syncControl.InvokeRequired)
{
//neller - if we're on the wrong thread, raise the
event on the correct thread.
_syncControl.Invoke(new
EventHandler(raiseResponse), dt, EventArgs.Empty);
}
else
{
//neller - if we're already on the correct thread,
just raise the event.
raiseResponse(dt, EventArgs.Empty);
}
}
}
}

//Rajat - In the following usercontrol we need to bind the data
received from our own event -- raiseResponse
public class GridControl : System.Windows.Forms.UserControl
{
private DataGrid dataGrid1 = new DataGrid();

Class1 instClass1;
DataTable dt = new DataTable(); //Rajat - It has some of the
same columns received from the other class, Not creating the full
structure for the table.

//Rajat - Constructor
public GridControl ()
{
instClass1 = new Class1(this);
instClass1.raiseResponse += new
EventHandler(instClass1_raiseResponse1);
dataGrid1.DataSource = dt;
}

public void instClass1_raiseResponse1(object sender, EventArgs
e)
{
MessageBox.Show("instClass1_raiseResponse1: " +
this.InvokeRequired.ToString());

DataTable dt = (DataTable)sender; //Rajat - I think
assigning to the datatable will update the datagrid.
dataGrid1.DataSource = dt; //neller - assigning will not
update the grid, you have to change the data source
}
}
}

Colin Neller
http://www.colinneller.com/blog
 

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