Listen event on different thread and update data on UI thread

R

Rajat

Hi,

I have a class in which I am receiving a event continuously at a great
speed. I need to listen to that event and show it on the grid in the GUI.
The problem is that the event is very very fast.

How can I listen to this event in a separate thread. Then take the data and
put it in the DataTable. I can send this DataTable back to the GUI thread
where I have a windows form on which I have applied the grid control. Please
let me know if I am not clear.

Please help.

Regards,
Rajat.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Rajat,

You can switch execution to the UI thread by calling Control.Invoke or
Control.BeginInvoke.
 
R

Rajat

Hi,

Thanks for replying. I have played with Control.Invoke and
Control.InvokeRequired a bit. As I described in the prblem, I am getting
continuous strem
in a different class. I have pasted a sample code and description below to
give you the glimpse of the problem.

Form1 with DataGrid1
-----------------------
On this form we have a datagrid which is binded to a DataTable.

Other Class (Level2Data)
--------------------------

I have tried to create a simulator using the timer to continuously generate
the event (which I am getting anyway). The actual work start at
Level2Data_ContinuousStream. If we could listen to this event on a different
thread and pass the received datatable to the Form1 for copying into
DataTable (of Form1). As the grid is binded to DataTable on form1 then it
will display the updated data from the event.

(Sorry for such a long post, but it was a bit difficult to tell the exact
issue. I hope that it will clear my problem.)

[Serializable]
public class Level2Data

{

const int noOfMMID = 27;

static string [] arrMMID = new
string[noOfMMID]{"BRUT","ABC","DEF","IJK","LMN","OPQ","RST","UVW","XYZ","BRU
T1","ABC1","DEF1","IJK1","LMN1","OPQ1","RST1","UVW1","XYZ1","BRUT2","ABC2","
DEF2","IJK2","LMN2","OPQ2","RST2","UVW2","XYZ2" };

static bool [] check = new bool[2]{true,false};

System.Timers.Timer timer1 = new System.Timers.Timer(30);

private event EventHandler ContinuousStream = null;

DataTable dtCopy = new DataTable();

//Constructor

public Level2Data()

{

timer1.Elapsed +=new System.Timers.ElapsedEventHandler(timer1_Elapsed);

this.ContinuousStream +=new EventHandler(Level2Data_ContinuousStream);

CreateTable();

timer1.Start();

}

private DateTime _enqueuetime;

public DateTime Enqueuetime

{

get{return _enqueuetime;}

set{_enqueuetime = value;}

}

string _symbol = string.Empty;

public string Symbol

{

get{return _symbol;}

set{_symbol = value;}

}

string _mmid = string.Empty;

public string Mmid

{

get{return _mmid;}

set{_mmid = value;}

}

bool _isBidPresent = false;

public bool IsBidPresent

{

get{return _isBidPresent;}

set{_isBidPresent = value;}

}

double _bidPrice = double.MinValue ;

public double BidPrice

{

get{return _bidPrice;}

set{_bidPrice = value;}

}

double _bidSize = double.MinValue ;

public double BidSize

{

get{return _bidSize;}

set{_bidSize = value;}

}

string _bidTime = string.Empty;

public string BidTime

{

get{return _bidTime;}

set{_bidTime = value;}

}

bool _isAskPresent = false;

public bool IsAskPresent

{

get{return _isAskPresent;}

set{_isAskPresent = value;}

}

double _askPrice = double.MinValue ;

public double AskPrice

{

get{return _askPrice;}

set{_askPrice = value;}

}

double _askSize = double.MinValue ;

public double AskSize

{

get{return _askSize;}

set{_askSize = value;}

}

string _askTime = string.Empty ;

public string AskTime

{

get{return _askTime;}

set{_askTime = value;}

}

private Level2Data getLevel2Data()

{

Level2Data instLevel2Data = new Level2Data();

Random rn = new Random();

instLevel2Data._mmid = arrMMID[rn.Next(1,noOfMMID)] ;

int nextindex = rn.Next(2);

if(check[nextindex] == true)

{

instLevel2Data._isBidPresent = true;

instLevel2Data._bidSize = 25 * rn.Next(100,500);

instLevel2Data._bidPrice = 300 * rn.NextDouble();

instLevel2Data._bidTime = DateTime.Now.ToString();

}


nextindex = rn.Next(2);

if(check[nextindex] == true)

{

instLevel2Data._isAskPresent = true;

instLevel2Data._askSize = 25 * rn.Next(100,500);

instLevel2Data._askPrice = 250 * rn.NextDouble();

instLevel2Data._askTime = DateTime.Now.ToString();

}

return instLevel2Data;

}

private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)

{

if(this.ContinuousStream !=null)

ContinuousStream(getLevel2Data(),EventArgs.Empty);

}

Hashtable mmidHash = new Hashtable();

private void Level2Data_ContinuousStream(object sender, EventArgs e)

{

bool toAdd = false;

try

{

Level2Data instLevel2 = (Level2Data) sender;

// DateTime currentTime = DateTime.Now;

// TimeSpan ts = currentTime.Subtract(instLevel2.Enqueuetime);

///Time interval after which feeds will be dropped

// if(ts.Milliseconds < 1000)

// {

string sym = instLevel2.Symbol.ToString();

//If this grid's symbol is same as that of the dequeued symbol then do task
else do nothing

if(!mmidHash.ContainsKey(instLevel2.Mmid)) //Add

{

mmidHash.Add(instLevel2.Mmid,dtCopy.Rows.Count); //
dsNew.Tables[0].Rows.Count);

object [] row = new object[9]{"","","","","","","","",""} ;

row[0] = instLevel2.Mmid.ToString();

if(instLevel2.IsBidPresent)

{

row[1] = instLevel2.BidPrice ;

row[2] = instLevel2.BidSize ;

row[3] = instLevel2.BidTime ; //pub.UTCToLocalTime(

row[4] = "" ; //GetColor(bprice);

toAdd = true;

}

else

{

row[1] = "";

row[2] = "";

row[3] = ""; //pub.UTCToLocalTime(

row[4] = "" ; //GetColor(bprice);

}

if(instLevel2.IsAskPresent)

{

row[5] = instLevel2.AskPrice ;

row[6] = instLevel2.AskSize ;

row[7] = instLevel2.AskTime ; // pub.UTCToLocalTime

row[4] = "" ; //GetColor(bprice);

toAdd = true;

}

else

{

row[5] = "";

row[6] = "";

row[7] = ""; // pub.UTCToLocalTime

row[4] = "" ; //GetColor(bprice);

}

// dsNew.Tables[0].Rows.Add(row);

if(toAdd == true)

dtCopy.Rows.Add(row); //dsCopy.Tables[0].Rows.Add(row); //
dtCopy.Rows.Add(row);

}

else //Update

{

int rowNo = Convert.ToInt32(mmidHash[instLevel2.Mmid]) ;

if(instLevel2.IsBidPresent)

{

// dsCopy.Tables[0].Rows[rowNo]["BidPrice"] = instLevel2.BidPrice;

// dsCopy.Tables[0].Rows[rowNo]["BidSize"] = instLevel2.BidSize;

// dsCopy.Tables[0].Rows[rowNo]["BidTime"] = instLevel2.BidTime ;

dtCopy.Rows[rowNo]["BidPrice"] = instLevel2.BidPrice;

dtCopy.Rows[rowNo]["BidSize"] = instLevel2.BidSize;

dtCopy.Rows[rowNo]["BidTime"] = instLevel2.BidTime ;

// dt.Rows[rowNo]["BIDCOLOR"] = GetColor(bprice);

}

if(instLevel2.IsAskPresent)

{

dtCopy.Rows[rowNo]["AskPrice"] = instLevel2.AskPrice;

dtCopy.Rows[rowNo]["AskSize"] = instLevel2.AskSize;

dtCopy.Rows[rowNo]["AskTime"] = instLevel2.AskTime ;

// dsCopy.Tables[0].Rows[rowNo]["AskPrice"] = instLevel2.AskPrice;

// dsCopy.Tables[0].Rows[rowNo]["AskSize"] = instLevel2.AskSize;

// dsCopy.Tables[0].Rows[rowNo]["AskTime"] = instLevel2.AskTime ;

// dt.Rows[rowNo]["BIDCOLOR"] = GetColor(bprice);

}

}


// dsCopy.AcceptChanges();

// DataSet checkDS = dsCopy.GetChanges();

// if(ultraGrid1.InvokeRequired)

// ultraGrid1.Invoke(new PassDataHandler(SetDisplayText),new
object[1]{dtCopy}); //checkDS}// dtCopy}); //dsnew

// else

// SetDisplayText(dtCopy) ;//;checkDS); //dtCopy) ;

}

catch(Exception ex)

{

string s =ex.Message + ex.Source + ex.StackTrace;

}


}

private void CreateTable()

{

DataColumn newDataColumn = new DataColumn("MMID");

dtCopy.Columns.Add(newDataColumn);


dtCopy.Columns.Add("BidPrice");

dtCopy.Columns.Add("BidSize");

dtCopy.Columns.Add("BidTime");

dtCopy.Columns.Add("BidColor");

dtCopy.Columns.Add("AskPrice");

dtCopy.Columns.Add("AskSize");

dtCopy.Columns.Add("AskTime");

dtCopy.Columns.Add("AskColor");

DataColumn [] dcPrimary = new DataColumn[1]{newDataColumn};

dtCopy.PrimaryKey = dcPrimary;

}

}


Regards,
Rajat.

Stoitcho Goutsev (100) said:
Rajat,

You can switch execution to the UI thread by calling Control.Invoke or
Control.BeginInvoke.


--

Stoitcho Goutsev (100) [C# MVP]

Rajat said:
Hi,

I have a class in which I am receiving a event continuously at a great
speed. I need to listen to that event and show it on the grid in the GUI.
The problem is that the event is very very fast.

How can I listen to this event in a separate thread. Then take the data
and
put it in the DataTable. I can send this DataTable back to the GUI thread
where I have a windows form on which I have applied the grid control.
Please
let me know if I am not clear.

Please help.

Regards,
Rajat.
 

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