title: Occasional issue with asynchronous callback
body: <p>I have a process that runs asynchronously every morning to download a file from an FTP site. The process works fine most of the time, but occasionally it will not complete, so I have been trying to add logs to figure out what is happening. As far as I can tell, the callback just randomly won't execute. Here is the relevant code:</p>
  
  <pre><code>Private Sub EndGetStreamCallback(ar As IAsyncResult)
      Dim state = CType(ar.AsyncState, FtpState)
  
      Try
          Dim responseStream As Stream = state.ResponseStream
          Dim bytesRead = responseStream.EndRead(ar)
          Dim result As IAsyncResult
          Dim complete = CInt((state.BytesRead / state.FileSize) * 100)
  
          If bytesRead &gt; 0 Then
              If complete &gt; 95 Then
                  Call InsertLog("ErrorLog", "EndGetStreamCallback: " &amp; bytesRead &amp; " bytes read. (" &amp; complete &amp; "%)")
              End If
  
              state.File.Write(state.Buffer, 0, bytesRead)
              state.BytesRead += bytesRead
              If _callback IsNot Nothing Then
                  _callback.StepTo(complete)
              End If
              lblCurrentProcess.Text = "Downloading Price Updates (" &amp; complete.ToString() &amp; "%)"
              result = responseStream.BeginRead(state.Buffer, 0, 4096, New AsyncCallback(AddressOf EndGetStreamCallback), state)
  
              If complete &gt; 95 Then
                  Call InsertLog("ErrorLog", "EndGetStreamCallback: after BeginRead()")
              End If
  
              If result Is Nothing Then
                  Call InsertLog("ErrorLog", "Fetch Price Updates: IAsyncResult Is Nothing")
              End If
              Return
          Else
              Call InsertLog("ErrorLog", "EndGetStreamCallback: 0 bytes read, file download complete.")
  
              state.File.Flush()
              state.File.Close()
              state.OperationComplete.Set()
              responseStream.Close()
              state.Response.Close()
  
              Call InsertLog("ErrorLog", "Beginning to apply price udpate.")
              Call ClearSemaphore("PriceUpdate")
              Call BeginPriceUpdate(state.FilePath)
          End If
      Catch ex As Exception
          Call InsertLog("ErrorLog", "EndGetStreamCallback (Price Update): " &amp; ex.Message, ex)
          MessageBox.Show("Could not get the request stream." &amp; ControlChars.CrLf &amp; ex.Message)
          state.OperationException = ex
          state.OperationComplete.Set()
          If _callback IsNot Nothing Then
              _callback.End()
          End If
          Call ClearSemaphore("PriceUpdate")
      End Try
  End Sub
  </code></pre>
  
  <p>As you can see, once the download completes, it should report <code>0 bytes read, file download complete</code> and begin to apply the update. However, sometimes it will just stop at 98, 99, or recently, I had it hit 100% and just not reach the completed section (the <code>Else</code>). I have been trying to figure this out for quite a while, but I'm not sure where else to look, as the method is just calling back to itself. No exceptions are thrown, it just stops (or hangs on another thread, I can't tell), but it does not complete once in a while. There doesn't seem to be any pattern that I'm noticing. Any insight would be greatly appreciated.</p>