Multithreading 运行时线程访问冲突错误
我的想法是从stringlist中的文件夹和子文件夹下载所有文件 接下来,我使用Multithreading 运行时线程访问冲突错误,multithreading,delphi-2010,Multithreading,Delphi 2010,我的想法是从stringlist中的文件夹和子文件夹下载所有文件 接下来,我使用SHGetFileInfo函数从要加载到Access数据库的文件中检索名称并键入日期和链接 我的应用程序运行良好,但当我使用一个包含数百个文件的大文件夹时,它会阻止我使用线程所需的内容 当我使用线程并且我的表为空时,它会显示错误消息,但第二次当我的表包含记录时,它显示没有问题 搜查程序 procedure FileSearche(const PathName: string; var lstFiles: TStrin
SHGetFileInfo
函数从要加载到Access数据库的文件中检索名称并键入日期和链接
我的应用程序运行良好,但当我使用一个包含数百个文件的大文件夹时,它会阻止我使用线程所需的内容
当我使用线程并且我的表为空时,它会显示错误消息,但第二次当我的表包含记录时,它显示没有问题
搜查程序
procedure FileSearche(const PathName: string; var lstFiles: TStringList);
const
FileMask = '*.*';
var
Rec: TSearchRec;
Path: string;
begin
Path := IncludeTrailingBackslash(PathName);
if FindFirst(Path + FileMask, faAnyFile - faDirectory, Rec) = 0 then
try
repeat
lstFiles.Add(Path + Rec.Name);
until FindNext(Rec) <> 0;
finally
FindClose(Rec);
end;
if FindFirst(Path + '*.*', faDirectory, Rec) = 0 then
try
repeat
if ((Rec.Attr and faDirectory) <> 0) and (Rec.Name <> '.') and
(Rec.Name <> '..') then
FileSearche(Path + Rec.Name, lstFiles);
until FindNext(Rec) <> 0;
finally
FindClose(Rec);
end;
end;
执行线程
procedure TForm1.Button1Click(Sender: TObject);
BEGIN
with debloc.Create do
FreeOnTerminate:=true;
END;
当我使用线程且表为空时,它会显示错误消息
违反地址规定00732BB1
但是第二次,当我的表被保存时,这不是一个问题
注意:尽管这段代码让我很恼火,但应用程序的工作原理是一样的
另一件事,我不知道如何停止线程时,文件夹是非常大的。我关闭停止的应用程序。我通过用listview组件替换dbgrid组件解决了这个问题
procedure debloc.transfertdata;
var
Myitem : TListItem;
MyColumn : TListColumn;
begin
ListView1.Items.Clear;
ListView1.Columns.Clear;
MyColumn:= ListView1.Columns.Add;
MyColumn.Caption:= 'Nom' ;
MyColumn.Width := -1;
MyColumn:= ListView1.Columns.Add;
MyColumn.Caption:= 'Type' ;
MyColumn.Width := -1;
MyColumn:= ListView1.Columns.Add;
MyColumn.Caption:= 'Taille' ;
MyColumn.Width := -1;
MyColumn:= ListView1.Columns.Add;
MyColumn.Caption:= 'Date de modification' ;
MyColumn.Width := -1;
MyColumn:= ListView1.Columns.Add;
MyColumn.Caption:= 'Lien' ;
MyColumn.Width := -1;
FDTable1.First;
while not FDTable1.Eof do
begin
ListView1.Items.BeginUpdate;
Myitem := ListView1.items.Add;
Myitem.Caption:= FDTable1.FieldByName('nom_file').ASSTRING;
Myitem.SubItems.Add(FDTable1.FieldByName('type_file').ASSTRING) ;
Myitem.SubItems.Add(FDTable1.FieldByName('size_file').ASSTRING) ;
Myitem.SubItems.Add(FDTable1.FieldByName('date_time_file').ASSTRING) ;
Myitem.SubItems.Add(FDTable1.FieldByName('lien_file').ASSTRING) ;
FDTable1.Next;
ListView1.Items.EndUpdate;
end;
end;
在我添加的线程中
Synchronize(transfertdata);
无法从线程访问UI对象。将所有代码移出线程。即使您修复了代码中的错误。正如您看到的,我可以在此应用程序中使用线程,您无法从线程访问
Form1
。您也无法访问Form1.StatusBar1
或其面板,或任何与用户界面相关的内容(根本没有VCL控件)。这在帮助中有文档记录,在以前的线程问题中已经讨论了至少几十次,并且可以通过简单的谷歌搜索轻松获得。如果您在IDE中使用File->New->Other->Thread Object
,在生成代码顶部的一个大注释块中,源代码中也会提到它。如果所有这些都不清楚,除了在主线程中,不要触摸任何视觉控件。
Synchronize(transfertdata);