Using the lock method correctly with Async BeginRead method
<p>I have a problem with the lock. I can recieve datas from the server, but when the server sends multiple datas, the callback method reads the data and reading the next data before doing anything.</p>

<p>Example:</p>

<pre><code>private void start()
{
    networkStream.BeginRead(buffer, 0, buffer.Length,
                            new AsyncCallback(dataRecievedCallback), null);
}

 private void dataRecievedCallback(IAsyncResult rst)
 {
     try
     {
         int currentDataSize = networkStream.EndRead(rst);

         string currentData = convertToString(buffer, currentDataSize);

         srvDataWorker.RecievedData.Content += currentData;
         srvDataWorker.RecievedData.Size += currentDataSize;

         if (IsValidXml(srvDataWorker.RecievedData.Content))
         {
             if (ServerConfig.SrvConfig.SaveIncomingData)
                 saveIncomingData(srvDataWorker.RecievedData.Content);
</code></pre>

<p>Here I am parsing the recieved xml data.</p>

<pre><code>             parseXmlFeed(srvDataWorker.RecievedData.Content);
</code></pre>

<p>Here I am sending recieved data to all clients.</p>

<pre><code>             sendToAllClients(srvDataWorker.RecievedData.Content);
             srvDataWorker.RecievedData.clear();
         }
         else
         {
             Display.Write("Data Waiting...", true);
         }

         clearBuffer();

         networkStream.BeginRead(buffer, 0, buffer.Length, 
                                 new AsyncCallback(dataRecievedCallback), null);
     } 
     catch (Exception ex)
     {
        //exception... 
     }
 }
</code></pre>

<p>I have tried the above code setting <strong>lock</strong> method, but it still does not helping me.</p>

<p>Where should I put the <strong>lock</strong>, after <strong>clearBuffer();</strong> Method?</p>

<p>example:</p>

<pre><code> lock(this)
 {
      networkStream.BeginRead(buffer, 0, buffer.Length, 
                              new AsyncCallback(dataRecievedCallback), null);
 }
</code></pre>

<p>or before the <strong>clearBuffer();</strong>, every code but not the BeginRead method?</p>

<p>Thank you...</p>
