[Lazarus] Free Pascal and Lazarus learning center

Anthony Walter sysrpl at gmail.com
Mon Feb 1 13:44:00 CET 2016


Just a bit of information, my Cross.Codebot library has threading built in
to the socket protocol implementations, so with regards to explaining it I
could just show the source code or I could write it a again just to
demonstrate.

With regards to complexity, I never define a new thread class, which is the
typical way most people think about working with threads in Free Pascal.
Instead I have one thread class defined in my System unit, and you pass it
your execute method along with an optional status handler. Your execute is
called, the thread object is freed for you automatically, and you don't
need to declare a new type.

procedure TDownloadForm.DownloadComplete(Thread: TSimpleThread);
begin
  FThread := nil;
  // Invoked in the user interface thread
  // Check Thread.Cancelled here
end;

procedure TDownloadForm.DownloadStatus(Thread: TSimpleThread);
begin
  // Invoked in the user interface thread
  // Do something with Thread.Status here
end;

procedure TDownloadForm.Download(Thread: TSimpleThread);
begin
  // Download here in a thread
  // Set Thread.Status := SomeString to sync status updates with the UI
  // FThread.Cancel cancels the thread
  // Throw exception cancels the thread
end;

procedure TDownloadForm.DownloadClick(Sender: TObject);
begin
  if FThread <> nil then
    Exit;
  FThread := TSimpleThread.Create(Download, DownloadStatus,
DownloadComplete);
end;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20160201/498458ca/attachment-0003.html>


More information about the Lazarus mailing list