全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1770
推到 Plurk!
推到 Facebook!

关于如何关闭动态创建的线程问题.

答題得分者是:jow
rainker
一般會員


發表:6
回覆:1
積分:1
註冊:2007-10-09

發送簡訊給我
#1 引用回覆 回覆 發表時間:2008-07-02 01:59:02 IP:116.209.xxx.xxx 訂閱
线程中代码

Unit getinfo;

constructor getinfo.Create(h:string;vlist:TTreeview); //取id!!
begin
v:=h;
ViewList:=vlist;
FreeOnTerminate := True;
inherited create(false);
end;


function check(a:string):string;
begin
//…
form1.memo1.lines.add(a);
end;

procedure getinfo.Execute;
begin
Check(v);
end;


创建线程代码

Unit Unit1;
var
infothread:array of getinfo;

procedure Tform1.Threadstop;
begin
//如果是这里应该添加什么代码,能让创建的线程立即停止.
//我试过Terminate - Free 等都失败了.
//出现过 句柄无效 或 程序卡死 或 直到线程将任务处理完成.
//我想立即终止线程,让它不在工作,该在哪里添加代码?//英雄,救命吧..

end;

procedure Tform1.GetStart;
var
i,v: integer;
begin
SetLength(infothread,self.Memo1.Lines.Count); // 动态设置线程的数量
for i := 0 to self.Memo1.Lines.Count -1 do
begin
infothread[i] := getinfo.Create(self.Memo1.Lines.Strings[i],self.treeview1);
infothread[i].OnTerminate := Threadok;
end;
end;
end;
編輯記錄
rainker 重新編輯於 2008-07-02 01:59:57, 註解 無‧
jow
尊榮會員


發表:66
回覆:751
積分:1253
註冊:2002-03-13

發送簡訊給我
#2 引用回覆 回覆 發表時間:2008-07-02 13:22:25 IP:59.105.xxx.xxx 未訂閱
使用一個自訂的Thread, 安排其動作流程
應該可以達到你的需求

以下測試碼提供你參考

http://delphi.ktop.com.tw/download.php?download=upload/486b1190abf8e_Test070.zip


[code delphi]
unit fMain;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TBreakEvent = procedure(Sender: TObject; var IsBreak: Boolean) of object;

TMyThread = class(TThread)
private
FOnStart: TNotifyEvent;
FOnDone: TNotifyEvent;
FOnWorking: TNotifyEvent;
FOnCheckBreak: TBreakEvent;
FCountDown: Integer;
FIndex: Integer;
FOwner: TForm;
FOnBreak: TNotifyEvent;
protected
FX: Integer;
FY: Integer;
FColor: TColor;
procedure Execute; override;
procedure Draw;
public
constructor Create(AOwner: TForm; Index, CountDown: Integer);
property Index: Integer read FIndex;
property Owner: TForm read FOwner;
property CountDown: Integer read FCountDown;
property OnStart: TNotifyEvent read FOnStart write FOnStart;
property OnWorking: TNotifyEvent read FOnWorking write FOnWorking;
property OnDone: TNotifyEvent read FOnDone write FOnDone;
property OnCheckBreak: TBreakEvent read FOnCheckBreak write FOnCheckBreak;
property OnBreak: TNotifyEvent read FOnBreak write FOnBreak;
end;

TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
public
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

{ TMyThread }

constructor TMyThread.Create(AOwner: TForm; Index, CountDown: Integer);
begin
inherited Create(True);
FreeOnTerminate := True;
FOwner := AOwner;
FIndex := Index;
FCountDown := CountDown;
FX := (FIndex mod 5) * 150;
FY := (FIndex div 5) * 20;
FColor := RGB(Random($100),Random($100),Random($100));
Resume;
end;

procedure TMyThread.Draw;
var
C: TCanvas;
S: string;
R: TRect;
begin
if FOwner <> nil then
begin
S := Format('%d@(%d,%d)=%d',[FIndex,FX,FY,FCountDown]);
C := FOwner.Canvas;
C.Lock;
try
C.Brush.Color := FOwner.Color;
C.Brush.Style := bsSolid;
C.Font.Color := FColor;
C.Font.Height := 20 - 2;
R := Rect(0,0,150,C.TextHeight('0'));
OffsetRect(R,FX 100,FY);
C.TextRect(R,FX 100,FY,S);
finally
C.Unlock;
end;
end;
end;

procedure TMyThread.Execute;
var
IsBreak: Boolean;
begin
IsBreak := False;

if Assigned(FOnStart) then FOnStart(Self);
while not Terminated do
begin
Synchronize(Draw);

if Assigned(FOnCheckBreak) then FOnCheckBreak(Self, IsBreak);

if IsBreak then Break;

if Assigned(FOnWorking) then FOnWorking(Self);

Dec(FCountDown);

if FCountDown <= 0 then Break;

Application.ProcessMessages;

Sleep(10);


end;

Synchronize(Draw);

case IsBreak of
False: if Assigned(FOnDone) then FOnDone(Self);
else if Assigned(FOnBreak) then FOnBreak(Self);
end;

Terminate;

end;

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
begin
for I := 0 to 149 do
TMyThread.Create(Self,I,Random(200) 200);
end;

end.


[/code]
編輯記錄
jow 重新編輯於 2008-07-02 13:27:43, 註解 無‧
系統時間:2024-04-29 13:01:01
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!