Hi,
I am facing issue while consuming data in event in window application. Data received is not in same sequence as sent from class where event is raised. Here is complete scenario.
There are 2 threads. In one thread, I receive data packets over TCP continuously and put it in array. In other thread, I pick that data from array then process that data and then send it to front end. I am raising event to send data to front end.
Received packet contains numeric sequence number. When event is raised, same sequence number is sent but in front end numbers received are not in same sequence.
Here is complete code. I have replaced Socket related code.
namespace MyConnectionManager
{
public delegate void DataReceivedHandler(int seqNum);
public class MyConnection
{
public event DataReceivedHandler DataReceived;
private Thread receiver = null;
private Thread processor = null;
private delegate void RecieveDelegate(int seqNum);
private bool isSocketConnected = false;
public MyConnection()
{
}
public bool StartServer()
{
bool isStarted = false;
try
{
receiver = new Thread(new ThreadStart(EnqueData));
processor = new Thread(new ThreadStart(ProcessData));
isSocketConnected = true;
receiver.Start();
processor.Start();
isStarted = true;
}
catch (Exception ex)
{
throw ex;
}
return isStarted;
}
public bool StopServer()
{
bool isStopped = false;
try
{
isSocketConnected = false;
receiver = null;
processor = null;
isStopped = true;
}
catch (Exception ex)
{
throw ex;
}
return isStopped;
}
int dummyData = 1;
private void EnqueData()
{
try
{
while (dummyData <= 100000)
{
MyEnque(dummyData);
dummyData++;
}
}
catch (Exception ex)
{
Console.WriteLine("Exception :=> " + ex.Message);
}
}
private void ProcessData()
{
try
{
RecieveDelegatercvrdy = new RecieveDelegate(ReceiveReady);
while (isSocketConnected)
{
int recvd = MyDequeue();
if (recvd > 0)
{
rcvrdy.BeginInvoke(recvd, null, null);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Exception :=> " + ex.Message);
}
}
private void ReceiveReady(int seqNum)
{
try
{
if (DataReceived != null)
DataReceived(seqNum);
}
catch (Exception ex)
{
throw ex;
}
}
#region Circular Array
private int[] seqQueue = new int[1000000];
private int readSeq = 0;
private int writerSeq = 0;
private int restart = 0;
private void MyEnque(int Data)
{
try
{
if (writerSeq >= 999999)
{
writerSeq = 0;
restart = 1;
}
seqQueue[writerSeq] = Data;
writerSeq++;
}
catch (Exception ex)
{
throw ex;
}
}
private int MyDequeue()
{
int myOutput = null;
try
{
if (readSeq < writerSeq && lPacketQueue[readSeq] != null)
{
myOutput = seqQueue[readSeq];
readSeq++;
}
if (readSeq > writerSeq && lRestartArray == 1)
{
if (readSeq >= 999999)
{
readSeq = 0;
restart = 0;
}
if (lPacketQueue[readSeq] != null)
{
myOutput = seqQueue[readSeq];
readSeq++;
}
}
}
catch (Exception ex)
{
throw ex;
}
return myOutput;
}
#endregion
}
}
namespace MyMain
{
class Program
{
static MyConnection conn = new MyConnection();
static void Main(string[] args)
{
try
{
conn.DataReceived += conn_DataReceived;
System.Threading.Thread.Sleep(5000);
conn.StartServer();
Console.WriteLine("Waiting to finish....");
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("Exception :=> " + ex.Message);
}
}
static void conn_DataReceived(int seqNum)
{
try
{
Console.WriteLine("Received Seq Num :=> {0}", seqNum);
if(seqNum >= 100000)
conn.StopServer();
}
catch (Exception ex)
{
Console.WriteLine("Exception :=> " + ex.Message);
}
}
}
}
OUTPUT
Received Seq Num :=> 1
Received Seq Num :=> 2
Received Seq Num :=> 4
Received Seq Num :=> 6
Received Seq Num :=> 3
Received Seq Num :=> 7
Received Seq Num :=> 5
.....
Received Seq Num :=> 100000
Received Seq Num :=> 99956
Received Seq Num :=> 99999
Received Seq Num :=> 99998
Can anybody help me in receiving this data in same sequence as sent by server inside event?
Thanks and Regards,
What I have tried:
Please check the complete code that I have added in main section in question.