Recent

Author Topic: Thread does only work once?  (Read 5185 times)

Yakumo500

  • New Member
  • *
  • Posts: 30
Thread does only work once?
« on: March 13, 2011, 09:41:38 pm »
Hi,
I have a problem with TThread.
I have a form with a button that executes a thread.
The problem is that only once the thread is called although i click multiple times on the button.

Sample source
http://ptp-systems.bplaced.net/Test.zip

eny

  • Hero Member
  • *****
  • Posts: 1646
Re: Thread does only work once?
« Reply #1 on: March 13, 2011, 10:08:15 pm »
Better study the tutorial first before trying to get something working by trial and error.
One of the most important lines on the page: "Do you need multi-threading?"

FWIW: your application will crash horribly because you are calling ShowMessage directly.
But that's all explained in the tutorial.
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

Yakumo500

  • New Member
  • *
  • Posts: 30
Re: Thread does only work once?
« Reply #2 on: March 14, 2011, 09:21:38 am »
Well ok this should be only an example.

When I change my code to this:
Code: [Select]
procedure test.execute();
begin
  Synchronize(@CallMainThread);
end;

procedure test.CallMainThread();
begin
  showmessage('hey');
end;

the behavior doesn't change. The messagebox is called only once although I press the button 2 or more times.

jixian.yang

  • Full Member
  • ***
  • Posts: 173
Re: Thread does only work once?
« Reply #3 on: March 14, 2011, 09:32:23 am »
Call "Create" instead of "Start".


procedure test.execute();
begin
  Synchronize(@CallMainThread);
end;

procedure test.CallMainThread();
begin
  showmessage('hey');
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  es := test.Create(True);
  es.FreeOnTerminate := True;
  es.start;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  //es := test.Create(True);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  //es.Free;
end;             

Yakumo500

  • New Member
  • *
  • Posts: 30
Re: Thread does only work once?
« Reply #4 on: March 15, 2011, 11:21:15 am »
Thanks for the answer. Now it works!

 

TinyPortal © 2005-2018