title: Problem with running WebService in separate thread in Delphi
body: <p>I have never asked questions in any community as I always solved problems by myself or could find them online. But with this one I came to dead end and need Help!
  To make it very clear – I converted a simple app, found elsewhere to make it use a Tthread object.
  The idea is simple – the app checks online using webservice, through THTTPRIO component, weather and put the results in Memo1 lines.</p>
  
  <p>Clicking on Button1 we get it done in standard way – using THTTPRIO put on the Form1 (it's called here htt as in original app) and using main and only thread.</p>
  
  <pre><code>procedure TForm1.Button1Click(Sender: TObject);
  var
  wf:WeatherForecasts;
  res:ArrayOfWeatherData;
  i:integer;
  begin
      wf:=(htt as WeatherForecastSoap).GetWeatherByPlaceName(edit1.Text);
      if wf.PlaceName&lt;&gt; '' then
      res:=wf.Details;
      memo1.Lines.Add('The min and max temps in Fahrenheit is:');
      memo1.Lines.Add(' ');
      for i:= 0 to high(res) do
      begin
          memo1.Lines.Add(res[i].Day+'   -   '+ ' Max Temp. Fahr: '+res[i].MaxTemperatureF+'   -   '+'Min Temp Fahr: '+res[i].MinTemperatureF);
      end
  end;
  </code></pre>
  
  <p>Clicking on Button2 – we use class TThread </p>
  
  <pre><code>procedure TForm1.Button2Click(Sender: TObject);
  var WFThread:WeatherThread;
  begin
    WFThread := WeatherThread.Create (True);
    WFThread.FreeOnTerminate := True;
    WFThread.Place := Edit1.Text;
    WFThread.Resume;
  end;
  </code></pre>
  
  <p>In Execute procedure in WeatherThread1 unit I put this code:</p>
  
  <pre><code>procedure WeatherThread.Execute;
  begin
    { Place thread code here }
    GetForecast;
    Synchronize (ShowWeather);
  end;
  </code></pre>
  
  <p>...and the GetForecast code:</p>
  
  <pre><code>procedure WeatherThread.GetForecast;
  var
      HTTPRIO: THTTPRIO;
      wf:WeatherForecasts;
      res:ArrayOfWeatherData;
      i:integer;
  begin
      HTTPRIO := THTTPRIO.Create(nil);
      HTTPRIO.URL := 'http://www.webservicex.net/WeatherForecast.asmx';
      HTTPRIO.WSDLLocation := 'http://www.webservicex.net/WeatherForecast.asmx?WSDL';
      HTTPRIO.Service := 'WeatherForecast';
      HTTPRIO.Port := 'WeatherForecastSoap';
  
      wf:=(HTTPRIO as WeatherForecastSoap).GetWeatherByPlaceName(Place);
  
      if Lines=nil then Lines:=TStringList.Create;
  
      if wf.PlaceName&lt;&gt; '' then
      res:=wf.Details;
      Lines.Clear;
          for i:= 0 to high(res) do
      begin
          Lines.Add(res[i].Day+'   -   '+ ' Max Temp. Fahr: '+res[i].MaxTemperatureF+'   -   '+'Min Temp Fahr: '+res[i].MinTemperatureF);
      end;
  end;
  </code></pre>
  
  <p>Procedure ShowWeather shows results in Form1.Memo1.
  And now there is a problem: In main thread, clicking Button1, everything works fine. But of course when HTTPRIO component communicates – it freezes the form. </p>
  
  <p>With Button2 I put the code in separate thread but it does NOT WANT TO WORK! Something strange happens. When I start application – and click Button2, there is an error when using HTTPRIO component. But it works for a while when I click FIRST Button1 and AFTER THAT Button2 (but it works for a while, 5-7 clicks only).
  I suppose I do something wrong but cannot figure out where the problem is and how to solve it. It looks like the code in threaded unit is not thread-safe, but it should be. Please help how to make HTTPRIO work in a thread!!!</p>
  
  <p>You can find zipped full code <a href="http://keyland.pl/stack/WeatherServicesThread.zip" rel="nofollow noreferrer">here</a>.</p>