So jetzt mal der Code wie ich ihn bisher habe
Delphi-Quellcode:
type
TForm1=class(TForm)
Start:TButton;
Button3:TButton;
ListBox1:TListBox;
Label1:TLabel1;
Label2:TLabel2;
ListBox3:TListBox;
Edit1:TEdit;
Memo1:TMemo;
procedure StartCklick(Sender:TObject);
procedure Button3Click(Sender:TObject);
procedure FormCreate(Sender:TObject);
procedure ListBox1Click(Sender:TObject);
private
public
end;
TyMyThread=Class(TThread)
protected
Counter:integer;
procedure execute;override;
public
constructor create;
end;
var
Form7:TForm7;
Thread1,Thread2,Thread3,Thread4,Thread5,Thread6:TMyThread;
ID,TreadID:integer;
implementation
procedure TMyThread.Execute;
var
b:integer;
while not Terminated do
begin
sleep(100)
if b=1 then
begin
Form7.Memo1.Lines.Add('Thread Nr.0 ' +DataTimeToStr(Now) + 'Uhr');
end;
if b=2 then
begin
Form7.Memo1.Lines.Add('Thread Nr.1 ' +DataTimeToStr(Now) + 'Uhr');
end;
end;
end;
procedure TForm7.StartClick(Sender :TOject);
var
I:integer;
begin
ThreadID:=ID;
if ThreadID=1 then
begin
Thread1:=TMyThread.create;
end;
if ThreadID=2 then
begin
Thread2:=TMyThread.create;
end;
end;
procedure TForm7.Button3Click(Sender:TObject);
begin
if ID=1 then
begin
Thread1.Terminate;
Thread1.WaitFor;
Thread1.Free;
end;
if ID=2 then
begin
Thread2.Terminate;
Thread2.WaitFor;
Thread2.Free;
end;
end;
constructor TMyThread.create;
begin
inherited create(True);
resume;
end;
procedure TForm7.FormCreate(Sender:TObject);
begin
Listbox1.Items.AddObject('Thread Nr. 0', TObject(1));
Listbox1.Items.AddObject('Thread Nr. 1', TObject(2));
end;
procedure 7TForm7.ListBox1Click(Sender: TObject);
begin
ID:= integer(ListBox1.Items.Objects(ListBox1.ItemIndex));
end;
end.
Das wäre bis jetzt der ganze Code, muss natürlich noch ordentlich überarbeitet werden, aber ich denke soweit funktioniert es jetzt so, dass ich 2 Threads starten/stoppen kann und man dies in der Ausgabe sieht.