title: Threading a .Net web service and providing a repsonse before continuing processing
body: <p>Let me start with the fact that similar forms of this question have been asked here, yet there are no working examples or useable answers to the subject (that I have been able to find). This post will attempt to provide a more concise example and advance a code block to that end. With reference to the following links that are similar though not conclusive/working examples.</p>
  
  <p><a href="https://stackoverflow.com/questions/228775/net-web-service-backgroundworker-threads">similar link #1 Background worker</a></p>
  
  <p><a href="https://stackoverflow.com/questions/17579406/multi-threading-in-web-service">similar link #2 Multi Threading in web service</a></p>
  
  <p><a href="https://stackoverflow.com/questions/1824933/right-way-to-create-thread-in-asp-net-web-application">similar link #3 threading in ASP.net</a></p>
  
  <p><a href="https://stackoverflow.com/questions/2356899/net-webservice-acknowledge-quickly-but-continue-processing-in-background">similar link #4 acknowledge quickly but continue processing</a></p>
  
  <p>Other links:</p>
  
  <p><a href="http://www.codeproject.com/Articles/600926/Asynchronous-web-services-call-in-ASP-NET" rel="nofollow noreferrer">#1 Async Web service Call</a></p>
  
  <p><a href="http://weblogs.asp.net/stevewellens/archive/2010/04/02/calling-web-service-functions-asynchronously-from-a-web-page.aspx" rel="nofollow noreferrer">#2 Calling Web Service Async functions from web page</a></p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/kbf0f1ct%28v=vs.90%29.aspx" rel="nofollow noreferrer">#3 MS ThreadPool.QueueUserWorkItem Method</a></p>
  
  <p><a href="http://msdn.microsoft.com/en-us/library/ywkkz4s1%28v=vs.90%29.aspx" rel="nofollow noreferrer">#4 MS Multithreading Walk Through Forms based</a></p>
  
  <p>The problem is returning a response to a client consuming a web service so that the client can continue on its way while the web service completes processing tasks. The web service (after authentication) receives a file name from the client that should be retrieved from a distant ftp server. Once the file has been downloaded to the web service hosting server, the client can be released, as the file name was valid and has been copied to the local server. </p>
  
  <p>The web server, after returning a simple 'true' Boolean response to the client, continues to crunch the downloaded file into a database and happily waits for the name of the next file.</p>
  
  <p>The current call is a SOAP message and this is a traditional asmx web service.</p>
  
  <p><strong>Web Service:</strong></p>
  
  <pre><code>&lt;WebService details .....
  
  Public sourceFile As String
  Public myData as Boolean = False
      &lt;WebMethod()&gt; _
  Public Function fileReady_Log2(ByVal user As String, ByVal PW As String, ByVal fileName As String) As String
  
      ' This function receives a web service call notifying this server to retrieve a file from a remote ftp server When the file is downloaded it is processed into this servers database.
      '
      ' This routine attempts to process an ASYNC routine to respond to the call and release the caller as soon as possible while continuing processing requirements.
  
      ' Validation removed for simplicity 
      If authorized Then
      ' Need to retrieve ftp server information and pull down the file and load it to the database.
          ' Call Async function
  
          sourceFile = filename
  
          Dim myObject = New ftpThread()
          myObject.SendDataAsync()
  
          ' Ok, now return the result to client.
          Return myData
  
          Dim ftpInfo As New ftpFileImport   '  continue with processing the uploaded file.
          results = ftpInfo.retrieveLogFile(fileName, deviceID, usr)
  
  
          Return results
      End If
      Return results
  End Function
  </code></pre>
  
  <p>Currently this is the structure of the calling functions. I have tried a number of variations and decided now is a good time to stop and ask for help.</p>
  
  <p><strong>ftpThread.vb</strong></p>
  
  <pre><code>Imports Microsoft.VisualBasic
  Imports System.Threading
  
  
  Public Class ftpThread
      Public Sub SendDataAsync()
          ThreadPool.QueueUserWorkItem(Sub(getFtpFileExists(sourceFile )))
      End Sub
  End Class
  </code></pre>
  
  <p>I am also getting an Expression Expected exception on the word Sub above.</p>
  
  <p><strong>ftp functions</strong> This process works synchronously, need it to be asynchronous.</p>
  
  <pre><code> Public Class ftpFileExistsCheck
        Public Sub getFtpFileExists()
  
          ' This all works without ASYNC 
  
             '  Retrieve FTP credentials from DB
             '  Set up ftp request
             '  download the file.
  
          '  But continuing process to DB at this point causes client to wait.
             myData  = True
       End Sub
   End Class
  </code></pre>
  
  <p>Thought I could figure this out from all I have read, but I just can't get past setting up the correct coding for ASYNC processing. </p>