Problem starting WebService in a separate thread in Delphi

I never asked questions in any community, as I always solved problems on my own or could find them on the Internet. But with this I came to a standstill and needed help! To make this very clear - I converted a simple application found elsewhere to use it in Tthread. The idea is simple - the application checks online, using the webservice, through the THTTPRIO component, the weather and puts the results in Memo1 lines.

By clicking on Button1, we will do it in the standard way - using THTTPRIO placed in Form1 (it is called htt here, as in the original application) and using the main and only stream.

procedure TForm1.Button1Click(Sender: TObject);
var
wf:WeatherForecasts;
res:ArrayOfWeatherData;
i:integer;
begin
    wf:=(htt as WeatherForecastSoap).GetWeatherByPlaceName(edit1.Text);
    if wf.PlaceName<> '' 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;

By clicking on Button2 - we use the TThread class

procedure TForm1.Button2Click(Sender: TObject);
var WFThread:WeatherThread;
begin
  WFThread := WeatherThread.Create (True);
  WFThread.FreeOnTerminate := True;
  WFThread.Place := Edit1.Text;
  WFThread.Resume;
end;

In the Execute procedure in the WeatherThread1 module, I put this code:

procedure WeatherThread.Execute;
begin
  { Place thread code here }
  GetForecast;
  Synchronize (ShowWeather);
end;

... GetForecast:

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<> '' 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;

ShowWeather Form1.Memo1. : , Button1, . , , HTTPRIO- - .

Button2 , ! - . - Button2, HTTPRIO . , FIRST Button1 AFTER THAT Button2 ( , 5-7 ). , - , , . , , . , , HTTPRIO !!!

zipped .

+3
3

Delphi 2007, madExcept . CoInitialize .
CoInitialize execute - .

procedure TWeatherThread.Execute;
begin
  CoInitialize(nil);
  try
     ...
  finally
    CoUninitialize;
  end;
end;
+4

, :

.

TThread.Synchronize method .

Delphi about .
Delphi 4, sortthds.pas ...\demos\threads, .

-

+1

You may be confusing the problem by creating dynamic RIO creation (RIO objects have weird lifetimes) and streams together, and comparing this result with a simple Button1. I would make another button that calls GetForecast without threads. See if this works. If it is bombing, then your problem is not cut.

0
source

Source: https://habr.com/ru/post/1758635/


All Articles