to Heytommy
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1234793
能否就告书我你在这个帖子的一段代码?谢谢。
1。因为我不知道如何send消息给线程,我只会sendmessage到主窗口,
procedure Tmainform.ShowSearchSuccess(var msg:Tmessage);
begin
:
Ins:=TinsertDataThread.create(FtpIp);
end;
a完成后,就通知主窗口就启动添加到数据库的线程TinsertDataTHREAD,不过,在TinsertDataSet还在添加
数据的时候,又有新的线程b通知主窗口启动TINSERTDATATHREAD,所以就同时有两个
TinsertDataThread在运行,所以我该怎么该呢?谢谢。
下面是添加线程的代码,不过,有点问题。
unit InsData;
interface
uses
Classes, ADODB,ActiveX,StrUtils,SysUtils;
type
TInsertDataThread = class(TThread)
private
{ Private declarations }
protected
ADODataSet: TADODataSet;
ADOCommand: TADOCommand;
ADOConnection: TADOConnection;
Ip,sName,Size,Time,DirName,TxtName:Ansistring;
F:TextFile;
procedure Execute;
override;
procedure AnalyzeDir(line:AnsiString);
procedure AnalyzeTxt(line:AnsiString);
public
constructor create(Sfilename:Ansistring);
Destructor Destroy;override;
end;
implementation
constructor TInsertDatathread.create(SFileName:AnsiString);
begin
inherited Create(true);
freeonterminate:=true;
CoInitialize(Nil);
ADOConnection := TADOConnection.Create( nil );
ADODataSet:= TADODataSet.Create(nil);
ADOCommand:=TADOCommand.Create(nil);
with AdoConnectiondo
begin
Provider := 'Provider=SQLOLEDB.1';
LoginPrompt := False;
KeepConnection := True;
ConnectionString := 'Provider=SQLOLEDB.1;Password=;Persist Security Info=;User ID=;Initial Catalog=;Data Source=';
connected:=True;
end;
with AdoDataSetdo
begin
Connection:=AdoConnection;
CacheSize:=1000;
locktype:=ltBatchOptimistic;
end;
{
with AdoCommanddo
begin
Connection:=AdoConnection;
prepared:=true;
end;
}
ip:=SFileName;
Resume;
end;
Destructor TInsertDataThread.Destroy;
begin
ADOCommand.Free;
ADOCommand:=nil;
ADOConnection.Free;
ADOConnection:=nil;
ADODataSet.Free;
ADODataSet:=nil;
CoUninitialize;
end;
procedure TInsertDataThread.Execute;
var
FileItem:string;
begin
DirName:=ip+'dir.txt';
txtName:=ip+'txt.txt';
if FIleExists(Dirname) then
begin
Assignfile(F,DirName);
Reset(F);
ADODataSet.close;
ADODataSet.CommandText:='select * from allml where 0=1';
ADODataSet.open;//出错,‘标记没有储存’?
try
begin
while not Eof(f)do
begin
Readln(F,FileItem);
AnalyzeDir(FileItem);
with ADODataSetdo
begin
insert;
FieldByName('ip').asString:=ip;
FieldByName('name').asString:=sname;
FieldByName('time').asString:=time;
post;
end;
{
begin
CommandText:='insert into allml(ip,name,time)'
+'values
ip,:sname,:time)';
Parameters.ParamByName('ip').Value :=ip;
Parameters.ParamByName('sname').Value :=sname;
Parameters.ParamByName('time').Value :=time;
// Execute;
end;
}
end;
AdoDataSet.UpdateBatch(arall);
end;
finally
closefile(f);
end;
end;
if FIleExists(txtname) then
begin
Assignfile(F,txtName);
Reset(F);
ADODataSet.close;
ADODataSet.CommandText:='select * from allfile where 0=1';
ADODataSet.open;
try
begin
while not Eof(f)do
begin
Readln(F,FileItem);
AnalyzeTxt(FileItem);
with ADODataSetdo
begin
insert;
FieldByName('name').asString:=sname;
FieldByName('size').asString:=size;
FieldByName('time').asString:=time;
FieldByName('ip').asString:=ip;
post;
end;
{
begin
CommandText:='insert into allfile(name,size,time,ip)'
+'values
sname,:size,:time,:ip)';
Parameters.ParamByName('sname').Value :=sname;
Parameters.ParamByName('size').Value :=size;
Parameters.ParamByName('time').Value :=time;
Parameters.ParamByName('ip').Value :=ip;
// Execute;
end;
}
end;
end;
finally
closefile(f);
end;
end;
AdoDataSet.UpdateBatch(arall);
end;