Witam. Piszę, ponieważ mam problem z wątkiem / programem.
Otóż posiadam prosty wątek:
unit Unit12;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, System.IOUtils, System.Threading,
Dialogs, Tlhelp32, StdCtrls, PsAPI, Vcl.Samples.Spin, Vcl.ExtCtrls, Math, Unit2, Unit3, Unit4, Unit5, Unit6, Unit7, Unit8, Unit9, Unit10, Unit11, Unit13, Unit14, Unit15,
uPlayer, Vcl.Menus, MMSystem;
type
TMSG = class(TThread)
private
protected
procedure Execute; override;
end;
var
Msg:TMSG;
implementation
Uses unit1;
procedure TMSG.Execute;
begin
FreeOnTerminate := true;
while not self.terminated do
begin
Form11.Listbox1.Items.Add(('Czas: ') + (FormatDateTime('HH:MM:SS:Z',Time)));
sleep(20000);
end;
end;
Initialization
MSG := TMSG.Create(True);
end.
Oraz wykorzystujący go checkbox:
procedure TForm1.CheckBox17Click(Sender: TObject);
begin
if CheckBox17.Checked then
begin
MSG.Resume;
end
else
begin
MSG.Suspend;
end;
end;
Wszystko działa mi dobre w momencie gdy z mojego głównego programu uruchomię formę 11. Lecz niestety program się zawiesza i wyłącza w momencie gdy tej formy nie uruchomię i tego listboxa nie widać.
W momencie gdy robię w głównej formie timer i wpisuję kod: Form11.Listbox1.Items.Add(('Czas: ') + (FormatDateTime('HHSS:Z',Time))); to żadnego problemu nie ma. Nic się nie zacina, a formy z listboxem nie muszę uruchamiać.
Dlaczego zatem cały program mi się wiesza jeżeli robię to z wątku?