[Lazarus] WinINet STDCALL callback crash
Sven Barth
pascaldragon at googlemail.com
Mon Feb 25 17:51:43 CET 2013
On 25.02.2013 17:42, matthew at accordancebible.com wrote:
> Ludo, Hans-Peter,
>
> Many thanks for your insights.
>
> Ludo, I am reviewing the msdn code example you posted; I have read quite a few but don't remember seeing that one.
>
> Hans-Peter, our project has so far been able to avoid the additional complexity posed by threads. It would be with reluctance that we would turn to threading; however, if experimenting with Ludo's post content (see below) does not work, then would will use threads. Might you know of an example implementation or code for asynchronous HTTP and/or FTP downloads?
In principle you could do it as follows:
=== example begin ===
type
TDownloader = class(TThread)
public type
TCallback = procedure(aSender: TDownloader; aData: TStream) of object;
private
fCallback: TCallback;
fUrl: String;
protected
procedure Execute; override;
public
constructor Create(const aUrl: String; aCallback: TCallback);
end;
constructor TDownloader.Create(const aUrl: String; aCallback: TCallback);
begin
// check that aCallback and aUrl are given and set up local variables
// ...
FreeOnTerminate := True;
inherited Create(False);
end;
procedure TDownloader.Execute;
var
ms: TMemoryStream;
begin
ms := TMemoryStream.Create;
try
// here you use what ever synchronous API you want and store it's
result in ms
// when all data is received
fCallback(Self, ms);
finally
ms.Free;
end;
end;
// somewhere else
TDownloader.Create('http://example.org/whatever', @MyCallback);
// fire and forget
=== example end ===
Note: if you want to be able to cancel the download (which could be done
by calling the thread's "Terminate" method which sets "Terminated" to
"True") you should not set "FreeOnTerminate" to "True", check for
"Terminated" in the thread's "Execute" method and capture the instance
returned by "TDownloader.Create".
Regards,
Sven
More information about the Lazarus
mailing list