unit threadguangpan;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Grids, DBGrids;
type
Ppath=^Tpath;
Tpath=record
path:string;
end;
TForm1 = class(TForm)
DBGrid1: TDBGrid;
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
wenjianNO:integer;
// Sect1:TRTLCriticalSection;
implementation
uses threadmodule;
{$R *.DFM}
function FindFile(path
ointer):Longint;stdcall;
var
SearchRec: TSearchRec;
path1
ointer;
err:integer;
begin
// EnterCriticalSection(Sect1);
try
err:=FindFirst(Ppath(path)^.path+'*.*',faAnyFile, SearchRec);
while err = 0do
begin
if SearchRec.Name[1]<>'.' then
begin
if ((SearchRec.Attr and fadirectory)>0) AND (SearchRec.Name[1]<>'.') then
begin
datamodule2.table1.Append;
datamodule2.table1.FieldByName('文件编号').asinteger:=wenjianNO;
datamodule2.table1.FieldByName('文件名称').asstring:=searchrec.Name;
datamodule2.table1.FieldByName('文件类型').asstring:='文件夹';
datamodule2.table1.Post;
wenjianNO:=wenjianNO+1;
try
path1:=Pointer(new(Ppath));
Ppath(path1)^.path:=Ppath(path)^.path+String(SearchRec.Name)+'/';
// LeaveCriticalSection(Sect1);
FindFile(path1);
except
exit;
end;
end
else
begin
datamodule2.table1.Append;
datamodule2.table1.FieldByName('文件编号').asinteger:=wenjianNO;
datamodule2.table1.FieldByName('文件名称').asstring:=searchrec.Name;
datamodule2.table1.FieldByName('文件类型').asstring:=ExtractFileExt(S
datamodule2.table1.FieldByName('所属文件夹').asstring:=Ppath(path)^.p
datamodule2.table1.Post;
wenjianNO:=wenjianNO+1;
end;
end;
err:= FindNext(SearchRec);
end;
finally
FindClose(SearchRec);
// LeaveCriticalSection(Sect1);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
hThread:THandle;
ThreadID
Word;
path
ointer;
begin
path:=Pointer(new(Ppath));
Ppath(path)^.path:='h:/';
Hthread:=CreateThread(nil,0,@FindFile,path,0,ThreadID);
if hthread=0 then
messageBox(Handle,'no Thread',nil,MB_OK);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
//InitializeCriticalSection(Sect1);
wenjianNO:=1;
end;
end.
会发生许多种错误,
最经常发生的是key violation,但我想不通怎么会发生这种错误。
还会出现的错误有:
内存不足;
Project Project1.exe raised edception class EAccessViolatio with
message 'Access violation at address 4CEOOC5B in module
'IDPDX32.DLL'.Read of address FFFFFFFF'.Process stopped. Use
Step or Run to continue.
'access violation at address 00401EE3 in module
'threadProject1.exe' Write of address 018282AC';
更有甚者,运行后连主窗体都看不见,
而且database desktop 无法启动。
大多数重启后,第一次运行此程序都是正常且正确的,但也有不行的时候。
以后几次是肯定不行了。
这个问题已困扰我好长时间了,
请各位打下一定要帮帮忙。