C# Programming
12-03-2009, 09:02 PM
I'm currently experiencing a race condition: my data-receive thread isn't ready by the time it's supposed to receive data
//create a thread to receive responses from the server
Thread clientThread = new Thread(new ParameterizedThreadStart(ProcessServerResponse));
clientThread.Start(RDclient);
//log activity to client window
logText("Connected to Server");
//initiate password syncronization - ProcessServerResponse() should be up and running by now
//TODO eliminate race condition
String clientRequest = "SYNC|" + strClientPassword;
Byte[] sendBytes = Encoding.ASCII.GetBytes(clientRequest);
tcpClientStream.Write(sendBytes, 0, sendBytes.Length);
tcpClientStream.Flush();
how do I ensure that my ProcessServerResponse() thread is at the .Read() before my main thread sends .Write ?
private void ProcessServerResponse(object client)
{
TcpClient _RDclient = (TcpClient)client;
NetworkStream tcpClientStream = _RDclient.GetStream();
String strTemp;
//data array to receive data
byte[] message = new byte[Properties.Settings.Default.iReceiveBufferSize];
int iBytesReceivedCount;
while (true)
{
iBytesReceivedCount = 0;
try
{
//blocks thread until server receives a message
iBytesReceivedCount = tcpClientStream.Read(message, 0, message.Length);
}
//create a thread to receive responses from the server
Thread clientThread = new Thread(new ParameterizedThreadStart(ProcessServerResponse));
clientThread.Start(RDclient);
//log activity to client window
logText("Connected to Server");
//initiate password syncronization - ProcessServerResponse() should be up and running by now
//TODO eliminate race condition
String clientRequest = "SYNC|" + strClientPassword;
Byte[] sendBytes = Encoding.ASCII.GetBytes(clientRequest);
tcpClientStream.Write(sendBytes, 0, sendBytes.Length);
tcpClientStream.Flush();
how do I ensure that my ProcessServerResponse() thread is at the .Read() before my main thread sends .Write ?
private void ProcessServerResponse(object client)
{
TcpClient _RDclient = (TcpClient)client;
NetworkStream tcpClientStream = _RDclient.GetStream();
String strTemp;
//data array to receive data
byte[] message = new byte[Properties.Settings.Default.iReceiveBufferSize];
int iBytesReceivedCount;
while (true)
{
iBytesReceivedCount = 0;
try
{
//blocks thread until server receives a message
iBytesReceivedCount = tcpClientStream.Read(message, 0, message.Length);
}