我有一个线程里面调用了一函数,该函数的作用是用递归算法遍历指定文件夹下的子目录。
如果在线程没执行完时,我将其挂起,我如何能将这个挂起的线程对象保存在硬盘里,在机器重启后可以继续执行这个未完成的线程
代码如下:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, ComCtrls,FileCtrl;type
TIndexThread = class(TThread) private
{ Private declarations }
protected
procedure Execute; override;
public
Bound,Start:string;
end;
type
TForm1 = class(TForm)
Button1: TButton;
btStop: TButton;
btStar: TButton;
RichEdit1: TRichEdit;
leDocment: TLabeledEdit;
leCurrPosition: TLabeledEdit;
OpenDialog1: TOpenDialog;
Label1: TLabel;
leStartTime: TLabeledEdit;
leStopTime: TLabeledEdit;
btContinue: TButton;
procedure btContinueClick(Sender: TObject);
procedure btStopClick(Sender: TObject);
procedure btStarClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
function IndexDocments(bound,start:string):boolean;
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
IndexThread:TIndexThread;
implementation{$R *.dfm}
procedure TForm1.btContinueClick(Sender: TObject);
begin
IndexThread.Resume;
end;procedure TForm1.btStarClick(Sender: TObject);
begin
richedit1.Clear;
leStartTime.Text:=datetimetostr(now());
IndexThread:=TIndexThread.Create(true);
IndexThread.Start:=leDocment.Text;
IndexThread.Bound:=leDocment.Text;
IndexThread.Resume;
//leCurrPosition.Text);
end;procedure TForm1.btStopClick(Sender: TObject);
var
tobject:TObject;
begin
if not IndexThread.Terminated then
IndexThread.Suspend;end;procedure TForm1.Button1Click(Sender: TObject);
var
mDir:string;
begin
if SelectDirectory('请选择目录','',mDir) then
leDocment.Text:=mDir;
end;function TForm1.IndexDocments(bound, start: string): boolean;
var
s:string;
DirInfo: TSearchRec;
Found:integer;
begin
result:=false;
{ if leCurrPosition.Text='' then
start:=bound; }
Found := FindFirst(start+'*' ,faAnyFile, DirInfo);
if Found<>0 then
exit; repeat
if (DirInfo.Attr and FaDirectory = FaDirectory) then //and (DirInfo.Attr and FaVolumeId <> FaVolumeID)) then if (DirInfo.Name<>'.') and (DirInfo.Name<>'..') then
begin
RichEdit1.Lines.Add(start+DirInfo.Name);
leCurrPosition.Text:=start+DirInfo.Name;
IndexDocments(bound,start+DirInfo.Name+'\');
end;
until Findnext(DirInfo)<>0; FindClose(DirInfo); label1.Caption:='文件夹个数:'+inttostr(richedit1.Lines.Count);end;{ TIndexThread }procedure TIndexThread.Execute;
begin
inherited;
Form1.IndexDocments(bound,start);
end;end.
如果在线程没执行完时,我将其挂起,我如何能将这个挂起的线程对象保存在硬盘里,在机器重启后可以继续执行这个未完成的线程
代码如下:
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, ComCtrls,FileCtrl;type
TIndexThread = class(TThread) private
{ Private declarations }
protected
procedure Execute; override;
public
Bound,Start:string;
end;
type
TForm1 = class(TForm)
Button1: TButton;
btStop: TButton;
btStar: TButton;
RichEdit1: TRichEdit;
leDocment: TLabeledEdit;
leCurrPosition: TLabeledEdit;
OpenDialog1: TOpenDialog;
Label1: TLabel;
leStartTime: TLabeledEdit;
leStopTime: TLabeledEdit;
btContinue: TButton;
procedure btContinueClick(Sender: TObject);
procedure btStopClick(Sender: TObject);
procedure btStarClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
function IndexDocments(bound,start:string):boolean;
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
IndexThread:TIndexThread;
implementation{$R *.dfm}
procedure TForm1.btContinueClick(Sender: TObject);
begin
IndexThread.Resume;
end;procedure TForm1.btStarClick(Sender: TObject);
begin
richedit1.Clear;
leStartTime.Text:=datetimetostr(now());
IndexThread:=TIndexThread.Create(true);
IndexThread.Start:=leDocment.Text;
IndexThread.Bound:=leDocment.Text;
IndexThread.Resume;
//leCurrPosition.Text);
end;procedure TForm1.btStopClick(Sender: TObject);
var
tobject:TObject;
begin
if not IndexThread.Terminated then
IndexThread.Suspend;end;procedure TForm1.Button1Click(Sender: TObject);
var
mDir:string;
begin
if SelectDirectory('请选择目录','',mDir) then
leDocment.Text:=mDir;
end;function TForm1.IndexDocments(bound, start: string): boolean;
var
s:string;
DirInfo: TSearchRec;
Found:integer;
begin
result:=false;
{ if leCurrPosition.Text='' then
start:=bound; }
Found := FindFirst(start+'*' ,faAnyFile, DirInfo);
if Found<>0 then
exit; repeat
if (DirInfo.Attr and FaDirectory = FaDirectory) then //and (DirInfo.Attr and FaVolumeId <> FaVolumeID)) then if (DirInfo.Name<>'.') and (DirInfo.Name<>'..') then
begin
RichEdit1.Lines.Add(start+DirInfo.Name);
leCurrPosition.Text:=start+DirInfo.Name;
IndexDocments(bound,start+DirInfo.Name+'\');
end;
until Findnext(DirInfo)<>0; FindClose(DirInfo); label1.Caption:='文件夹个数:'+inttostr(richedit1.Lines.Count);end;{ TIndexThread }procedure TIndexThread.Execute;
begin
inherited;
Form1.IndexDocments(bound,start);
end;end.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货