Click here to Skip to main content
15,908,013 members
Home / Discussions / C#
   

C#

 
QuestionDouble buffering ineffective Pin
Lauulauu28-Nov-07 23:25
Lauulauu28-Nov-07 23:25 
AnswerRe: Double buffering ineffective Pin
Luc Pattyn28-Nov-07 23:38
sitebuilderLuc Pattyn28-Nov-07 23:38 
AnswerRe: Double buffering ineffective Pin
Russell Jones29-Nov-07 1:01
Russell Jones29-Nov-07 1:01 
AnswerRe: Double buffering ineffective Pin
Ennis Ray Lynch, Jr.29-Nov-07 4:19
Ennis Ray Lynch, Jr.29-Nov-07 4:19 
QuestionC# Crystal Report Export Pin
The Geek28-Nov-07 23:04
The Geek28-Nov-07 23:04 
QuestionAnother UI Issue (ToolStrip) Pin
Jeffrey Walton28-Nov-07 22:38
Jeffrey Walton28-Nov-07 22:38 
AnswerRe: Another UI Issue (ToolStrip) Pin
Pankaj - Joshi28-Nov-07 22:47
Pankaj - Joshi28-Nov-07 22:47 
QuestionA Question about Threading with Socket-Class Pin
MarkPhB28-Nov-07 22:15
MarkPhB28-Nov-07 22:15 
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<List<byte>> 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<byte>( tmpEingangsPuffer ) );
              }
              else {
                  // add the mainBuffer-data to the receivedDataQueue
                  receivedDataQueue.Enqueue( new List<byte>( 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_ThreadMethode ) );
                  processingThread.IsBackground = true;
                  processingThread.Start();
              }

              // Signalize the processingThread that it can go further
              autoResetEvent_.Set();
            
        }
        else {
            // Close the Connection
            closeConnection();
        }

        // if the socket wasnt closed then start again the async receiving
        if( socketClosed == false ) {
        
            // clear MainBuffer.
            Array.Clear( mainBuffer, 0, mainBuffer.Length );

            // start async receiving
            connSocket.BeginReceive( mainBuffer, 0, mainBuffer.Length, SocketFlags.None, out socketFehler_, new AsyncCallback( receiveData ), connSocket );
        }
    }
    catch( SocketException ) {
        closeConnection();
    }
    catch( ThreadStateException ) {
        closeConnection();
    }
    catch( ObjectDisposedException ) { }
The second thread execute the following Methode
private void empfangsDatenVerarbeitung_ThreadMethode() {

// Loop until the Connection is alive
while( socketClosed  == false ) {

    // start only if there is something to do
    if( receivedDataQueue.Count > 0 ) {

        // loop until there is something to do
        while( receivedDataQueue.Count != 0 ) {
        
          // process the Data ...

        }

    }

    // Wait until some that are available
    autoResetEvent_.WaitOne();

}
-------------------------------------------------------

OK, it looks fine but the method "receiveData" wich should be raised automatically when data are available doesnt raise everytime. After a minute it starts to stuck =/...my question is now...

Is there something that i do wrong with the threading ?
AnswerRe: A Question about Threading with Socket-Class [modified] Pin
m@u28-Nov-07 22:48
m@u28-Nov-07 22:48 
QuestionClass library assembly permissions, best practice Pin
harleydk28-Nov-07 22:11
harleydk28-Nov-07 22:11 
Questionhow to add new row to an exiting data table...? Pin
Pankaj - Joshi28-Nov-07 21:26
Pankaj - Joshi28-Nov-07 21:26 
AnswerRe: how to add new row to an exiting data table...? Pin
Thaer Hamael28-Nov-07 21:33
Thaer Hamael28-Nov-07 21:33 
GeneralRe: how to add new row to an exiting data table...? Pin
Pankaj - Joshi28-Nov-07 22:09
Pankaj - Joshi28-Nov-07 22:09 
GeneralRe: how to add new row to an exiting data table...? Pin
Pankaj - Joshi28-Nov-07 22:12
Pankaj - Joshi28-Nov-07 22:12 
GeneralRe: how to add new row to an exiting data table...? Pin
Mr. Candyman28-Nov-07 23:03
Mr. Candyman28-Nov-07 23:03 
Jokesorry, duplicated message :-P Pin
Mr. Candyman28-Nov-07 23:05
Mr. Candyman28-Nov-07 23:05 
QuestionActive Control Pin
Thaer Hamael28-Nov-07 21:22
Thaer Hamael28-Nov-07 21:22 
QuestionDataGridView Pin
baerten28-Nov-07 21:19
baerten28-Nov-07 21:19 
AnswerRe: DataGridView Pin
Thaer Hamael28-Nov-07 21:39
Thaer Hamael28-Nov-07 21:39 
GeneralRe: DataGridView Pin
baerten28-Nov-07 21:45
baerten28-Nov-07 21:45 
GeneralRe: DataGridView Pin
Pankaj - Joshi28-Nov-07 22:19
Pankaj - Joshi28-Nov-07 22:19 
QuestionRun a C# program in Dos window Pin
.NET- India 28-Nov-07 21:12
.NET- India 28-Nov-07 21:12 
QuestionRe: Run a C# program in Dos window Pin
Maharishi Bhatia28-Nov-07 21:20
Maharishi Bhatia28-Nov-07 21:20 
AnswerRe: Run a C# program in Dos window Pin
baerten28-Nov-07 21:21
baerten28-Nov-07 21:21 
GeneralRe: Run a C# program in Dos window Pin
.NET- India 28-Nov-07 21:32
.NET- India 28-Nov-07 21:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.