title: running a thread from within a vb.net webservice
body: <p>Hi all I have a webservice with a webmethod that accepts an XML from a client via POST (don't know if this really matters), validates XML and the data inside, and then processes the XML and returns an answer to the client.</p>
  
  <p>At moment, I have a problem with large XMLs; it takes too long for the XML preprocessing to finish, and the clients timeout expires.</p>
  
  <p>Instead of increasing the timeout of the client I thought I could maybe separate the XML validation from the XML processing by starting a new thread after the validation which processes the XML and at the same time return the validation answer to the client. (the processing won't start if the XML is not valid.</p>
  
  <p>Now my question is: <b>Will opening the new thread increase memory usage? normally I would guess it will (for normal applications) but here I am returning an answer to the client and essentially ending the process of the main thread.</b></p>
  
  <p><s>And another question, I actually tried to create a small webservice that does just that but for some reason, even though the webmethod was finished, the client would not get an answer back until the thread died... am I doing something wrong?</s></p>
  
  <p>Ok, I rewrote the small webservice and it actully works (I haven't tried it on the real code yet - it's a tad more complicated).<br>
  This is the example code if anyone wants it:</p>
  
  <pre><code>Imports System.Web.Services
  Imports System.Web.Services.Protocols    
  Imports System.ComponentModel
  
  ' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
  ' &lt;System.Web.Script.Services.ScriptService()&gt; _
  &lt;System.Web.Services.WebService()&gt; _
  &lt;System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)&gt; _
  &lt;ToolboxItem(False)&gt; _
  Public Class DemoService
      Inherits System.Web.Services.WebService
  
         &lt;WebMethod()&gt; _
      Public Function XMLReceiver(ByVal XMLData As String) As Integer
          Dim retval As Integer = Validate(XMLData)
          Dim thrd As New Threading.Thread(AddressOf Process)
          thrd.Start()
          Return retval
      End Function
  
      Public Function Validate(ByVal XMLData As String) As Integer
          Return 0
      End Function
  
      Public Sub Process()
          'get time
          Dim start As Date = Date.Now
          'do work
          Threading.Thread.Sleep(1000 * 10)
          'notification (actually write to DB)
          Dim writer As New IO.StreamWriter("C:\done.txt")
          writer.WriteLine("From " &amp; start.ToLongTimeString &amp; " To " &amp; Date.Now.ToLongTimeString &amp; " done!")
          writer.Close()
      End Sub
  End Class
  </code></pre>
  
  <p>So Now it's just a matter of the first question.</p>
  
  <p>Thanks everyone</p>