We have Winforms based Dealer Workstation Application in C#.Net 3.5.
Its a real time socket based application.
The problem we are facing as follows:-
We have a UDP socket that listens Market Data Packet on a separate Thread , and on receiving the appropriate packet , raises an event that is handled in multiple forms where the market data is painted. Problem we are facing is at times the we finding delay
or miss of event handler triggering randomly across multiple listeners.
For Example:-
Packet MBP is received by the UDP Socket , the socket thread raises an event OnMBPBcastArrival(); which is fired on all the forms subscribing the event. In our application at times even though there are three forms that have registered the event the data is
painted on only two forms instead of three.We are unable to find out whether the event was not fired on the third form at all or is there some other issue that needs to be checked.
PFA snippet of the sample code
Function that receives UDP Packet and Fires an Event
private void OnBCastDataArrival(byte[] data)
{
switch (transcode)
{
case BroadcastTranscodes.TC_MBP_BCAST:
INT_MBP_BCAST MBP;
MBP = BcastStructureFactory<INT_MBP_BCAST>.GetStructure(data);
if (OnMBPBcastArrival != null)
OnMBPBcastArrival.Invoke(MBP);
BREAK;
}
}
EVENT SUBSCRIPTION IN THREE DIFFERENT FORMS
1] FrmBlotter
for Every form, object of class "clsBcastHandlerLayer"(which is private object of MDI) is passed in their corresponding constructor and is assigned to form level object of that class.
This form level object ( _bcastHandler) is used to subscribe the events on individual form as shown below.
//Constructor
public frmBlotter(clsBcastHandlerLayer bcastHandlerLayer)
{
//form level object is assgned
_bcastHandler = bcastHandlerLayer;
}
public void SubscibeEvents()
{
_bcastHandler.OnMBPBcastArrival += new clsBcastHandlerLayer.TC_MBP_BCAST_EventHandler(_bcastHandler_OnMBPBcastArrival);
}
2] FrmMktWatch
public frmMktWatch(clsBcastHandlerLayer bcastHandlerLayer)
{
//class level object is assgned
_bcastHandler = bcastHandlerLayer;
}
public void SubscibeEvents()
{
_bcastHandler.OnMBPBcastArrival += new clsBcastHandlerLayer.TC_MBP_BCAST_EventHandler(_bcastHandler_OnMBPBcastArrival);
}
3] FrmMBP
public frmMBP(clsBcastHandlerLayer bcastHandlerLayer)
{
//class level object is assgned
_bcastHandler = bcastHandlerLayer;
}
public void SubscibeEvents()
{
_bcastHandler.OnMBPBcastArrival += new clsBcastHandlerLayer.TC_MBP_BCAST_EventHandler(_bcastHandler_OnMBPBcastArrival);
}
On all the three forms we have function named "_bcastHandler_OnMBPBcastArrival" that is fired when the event is raised
Here BeginInvoke is used to asynchronously paint the UI , as incase of Invoke we faced the issue of deadlock due to which painting of real-time packet completely stops throughout the application.
void _bcastHandler_OnMBPBcastArrival(INT_MBP_BCAST MBP)
{
if (this.InvokeRequired)
{
//broadcast stopping issue
this.BeginInvoke(new broadCastDelegate(_bcastHandler_OnMBPBcastArrival), new object[] { MBP });
return;
}
//PAINTING OF DATA FROM PACKET HAPPENS HERE
}
We are not able to replicate in offshore environment here but we have seen it happen in production.