Forum > General
TThread - Why can't I start a thread again?
jamie:
Maybe it makes no difference here however, you did not specify to OVERRIDE the constructor in the base thread class.
Constructor Create(.....) override; <<<<
Scrub that!
jamie:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Grids; type TMyThread = Class(TTHread) Constructor Create(CreateSuspended:Boolean;Const StackSize:SizeUint=DefaultStackSize);Virtual; Procedure Execute; Override; end; { TForm1 } TForm1 = class(TForm) Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure FormCreate(Sender: TObject); procedure FormDestroy(Sender: TObject); private public MyTh:TMyThread; end; var Form1: TForm1; implementation {$R *.lfm}Procedure TMyThread.Execute;Var T,U:Int64;Begin T:=0; While Not Terminated do Begin U:= GetTickCount64; if Abs(U-T) > 2000 THen Begin T := U; Beep; end; end;end; Constructor TMyThread.Create(CreateSuspended:Boolean;Const StackSize:SizeUint=DefaultStackSize);Begin inherited Create(CreateSuspended,StackSize);end; { TForm1 } procedure TForm1.Button1Click(Sender: TObject);begin MyTh.Start;end; procedure TForm1.Button2Click(Sender: TObject);begin Myth.Suspend;end; procedure TForm1.FormCreate(Sender: TObject);begin MyTh := TMythread.Create(True); MyTh.FreeOnTerminate := False;end; procedure TForm1.FormDestroy(Sender: TObject);begin MyTh.Terminate; MyTh.Free;end; end.
This works for me.
You can also do this in the Execute Method by using a flag.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Procedure TMyThread.Execute;Var T,U:Int64;Begin T:=0; While Not Terminated do If Not Paused Then Begin U:= GetTickCount64; if Abs(U-T) > 2000 THen Begin T := U; Beep; end; end;end;
So you get the thread started and simply toggle the PAUSE boolean which is something you add to your Thread class in the public section.
This way the thread is always running and you can also yield to other threads using that check point..
If Not Terminated Then
If Not Paused then
Begin
.....
End Else Yield;
etc..
Slawek:
Thank you Jamie. Your explanation of this example seems understandable to me. I'll test it tonight :)
Navigation
[0] Message Index
[*] Previous page