A Question about Threading with Socket-Class
-
I work on a class that handles the a tcp-connection but now i discovered a problem with my data-handling and cant solve it =/ First some Informations: The Socket-class provides the Async-Methodes BeginReceive and EndReceive to receive the data over the socket. When i call BeginReceive then another Methode will automatically raise.
connSocket.BeginReceive( mainBuffer_, 0, mainBuffer_.Length, SocketFlags.None, out socketError_, new AsyncCallback( receivedata ), connSocket );
In this case the methode "receivedata" will raise when data are available. In this methode "receivedata" do i fetch the data from the socket and save it a Queue, after that i start a second thread wich process the data from the queue.
private SocketError socketError; private volatile byte[] mainBuffer; private volatile int receivedDataSize; private volatile Thread processingThread; private volatile AutoResetEvent autoResetEvent; private volatile Queue> receivedDataQueue; ... private void receiveData( IAsyncResult aSyncResult ) { try { // Extract the delivered Socket Socket connSocket = ( Socket ) aSyncResult.AsyncState; // End Asyncronen Operation receivedDataSize = connSocket.EndReceive( aSyncResult, out socketFehler_ ); // are data available ? if( receivedDataSize > 0 ) { // Is the receivedDataSize bigger than the ReceiveBufferSize? if( receivedDataSize != connSocket_.ReceiveBufferSize ) { // extract the data from the mainBuffer byte[] tmpBuffer = new byte[ receivedDataSize ]; Array.Copy( mainBuffer, tmpBuffer, receivedDataSize ); // add the mainBuffer-data to the receivedDataQueue receivedDataQueue.Enqueue( new List( tmpEingangsPuffer ) ); } else { // add the mainBuffer-data to the receivedDataQueue receivedDataQueue.Enqueue( new List( mainBuffer ) ); } // start second Thread when the connections is still alive and the second Thread is null or is not alive if( ( socketClosed == false ) && ( ( processingThread == null ) || ( processingThread.IsAlive == false ) ) ) { // Starte processing-Thread. processingThread = new Thread( new ThreadStart( empfangsDatenVerarbeitung_ThreadMeth
-
I work on a class that handles the a tcp-connection but now i discovered a problem with my data-handling and cant solve it =/ First some Informations: The Socket-class provides the Async-Methodes BeginReceive and EndReceive to receive the data over the socket. When i call BeginReceive then another Methode will automatically raise.
connSocket.BeginReceive( mainBuffer_, 0, mainBuffer_.Length, SocketFlags.None, out socketError_, new AsyncCallback( receivedata ), connSocket );
In this case the methode "receivedata" will raise when data are available. In this methode "receivedata" do i fetch the data from the socket and save it a Queue, after that i start a second thread wich process the data from the queue.
private SocketError socketError; private volatile byte[] mainBuffer; private volatile int receivedDataSize; private volatile Thread processingThread; private volatile AutoResetEvent autoResetEvent; private volatile Queue> receivedDataQueue; ... private void receiveData( IAsyncResult aSyncResult ) { try { // Extract the delivered Socket Socket connSocket = ( Socket ) aSyncResult.AsyncState; // End Asyncronen Operation receivedDataSize = connSocket.EndReceive( aSyncResult, out socketFehler_ ); // are data available ? if( receivedDataSize > 0 ) { // Is the receivedDataSize bigger than the ReceiveBufferSize? if( receivedDataSize != connSocket_.ReceiveBufferSize ) { // extract the data from the mainBuffer byte[] tmpBuffer = new byte[ receivedDataSize ]; Array.Copy( mainBuffer, tmpBuffer, receivedDataSize ); // add the mainBuffer-data to the receivedDataQueue receivedDataQueue.Enqueue( new List( tmpEingangsPuffer ) ); } else { // add the mainBuffer-data to the receivedDataQueue receivedDataQueue.Enqueue( new List( mainBuffer ) ); } // start second Thread when the connections is still alive and the second Thread is null or is not alive if( ( socketClosed == false ) && ( ( processingThread == null ) || ( processingThread.IsAlive == false ) ) ) { // Starte processing-Thread. processingThread = new Thread( new ThreadStart( empfangsDatenVerarbeitung_ThreadMeth
i'm not sure, but i think that Thread.IsAlive is a bit dangerous... msdn says about Thread.IsAlive : "true if this thread has been started and has not terminated normally or aborted otherwise false" so if an exception comes up in your dataprocessing you have a autoResetEvent that signals something and nobody listening. -- modified at 5:01 Thursday 29th November, 2007 never mind.. Thread.IsAlive does exactliy what it should :) you could put the stuff into a console application and do some Console.WriteLines to find out where it actually stops working..