[Lazarus] About Threads

Kjow antispammoni at gmail.com
Wed Sep 21 20:28:43 CEST 2011


Hi all,

I need to unlock my program when I run a function (it simply loads a
file and elaborates some data, I would do something of beauty like a
progress bar).
I read the wiki, but it seems to be too much for my purpose and the
function I need to run is already inside a class (I wouldn't touch
it).

On delphibasics I read about BeginThread (
http://www.delphibasics.co.uk/RTL.asp?Name=BeginThread ) and I'm
trying to implement it on a trivial test project (it just insert in
two TMemo the "i" of a loop), e.g.:

(********************************************)
...
function count(n: integer; memo: Tmemo):Integer;
var
  i: integer;
begin
  memo.Clear;
  for i:= 0 to n do
    begin
      memo.Append(IntToStr(i));
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  count(1000,memo1);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  count(1000,memo2);
end;
...
(********************************************)

The goal is to run "count" in a thread and BeginThread seems to be ok
for this, but I have some problems with delphibasics code:

(********************************************)
...
function ShowMsg(Parameter : Pointer) : Integer;
begin
  // Set up a 0 return value
  Result := 0;

  // Map the pointer to the passed data
  // Note that each thread has a separate copy of msgPtr
  msgPtr := Parameter;

  // Display this message
  //ShowMessagePos('Thread '+IntToStr(msgPtr.thread)+' '+msgPtr.msg, //original
  //               200*msgPtr.thread, 100);
                              //original

  ShowMessagePos('Thread '+IntToStr(msgPtr^.thread)+' '+msgPtr^.msg,
                 200*msgPtr^.thread, 100);

  // End the thread
  EndThread(0);
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  id1, id2 : LongWord;
  thread1, thread2 : Integer;
  msg1, msg2 : TMsgRecord;

begin
  // set up our display messages
  msg1.thread := 1;
  msg1.msg    := 'Hello World';
  msg2.thread := 2;
  msg2.msg    := 'Goodbye World';

  // Start the first thread running asking for users first name
  thread1 := BeginThread(nil,
                         0,
                         Addr(ShowMsg), //I get error here(*)
                         Addr(msg1),
                         0,
                         id1);

  // And also ask for the surname
  thread2 := BeginThread(nil,
                         0,
                         Addr(ShowMsg), //I get error here(*)
                         Addr(msg2),
                         0,
                         id2);

  // Ensure that the threads are only closed when all done
  ShowMessagePos('Press this when other dialogs finished.', 200, 300);

  // Finally, tidy up by closing the threads
  CloseHandle(thread1);
  CloseHandle(thread2);
end;
...
(********************************************)


(*)I get error here: main.pas(122,31) Error: Wrong number of
parameters specified for call to "ShowMsg"

How to fix?

There is a better/easier way?

Thank you,
Kjow




More information about the Lazarus mailing list