J
junrui2726
Unregistered / Unconfirmed
GUEST, unregistred user!
我写了一个线程(MyTread),这个线程从一张表里一次取出一条数据,用memo显示出来后进行处理,然后就把该条数据删除,后把删除的数据插入到一个备份表里。要求是每一秒钟处理100条,也就是在21秒处理100条,22秒有处理100条。怎么在一秒钟内同时启动100个MyTread线程呢?是在同一秒?? 下面是我简单写的线程,没实用性,只是拿来说明而已。
MyTread = class(TThread)
private
FADOConnection :TADOConnection;
FADOQuery :TADOQuery;
FDataSource :TDataSource;
protected
procedure Execute;
override;
end;
implementation
procedure MyTread.Execute;
var
i :integer;
DispMemo :string;
insertStr :string;
begin
freeonterminate:=true;
while not Terminateddo
begin
CoInitialize(0);
FADOConnection := TADOConnection.Create(nil);
FADOQuery := TADOQuery.Create(nil);
FDataSource := TDataSource.Create(nil);
try
FADOConnection.Close;
FADOConnection.ConnectionString := 'Provider=SQLOLEDB.1;Password=22;Persist Security Info=True;User ID=sa;Initial Catalog=sas;Data Source=HJ';
FADOConnection.Open;
FADOQuery.Connection := FADOConnection;
FDataSource.DataSet :=FADOQuery;
form1.Edit1.Text :=timetostr(time);
EnterCriticalSection(CriticalMy);//进入临界段
FADOQuery.Close;
FADOQuery.SQL.Clear;
FADOQuery.SQL.Add('select top 1 id,nnr,csr,gzr,bjr,tjr,');
FADOQuery.sql.Add('hnr,hbr,slr,dlr,xhr,fre,fgf,tuiff,');
FADOQuery.sql.Add(' from hj1 order by Priority desc');
FADOQuery.Open;
if FADOQuery.RecordCount =0 then
exit;
DispMemo:=' [ '+timetostr(time)+' ] '
+midstr(FADOQuery.FieldByName('nnr').AsString,3,length(FADOQuery.FieldByName('nnr').AsString))+' '
+FADOQuery.FieldByName('bjr').AsString+' '
+format('%x',[FADOQuery.FieldByName('hnr').asinteger])+' '
+FADOQuery.FieldByName('slr').AsString+' '
+FADOQuery.FieldByName('tuiff').AsString;
form1.Memo1.Lines.Add(DispMemo);
fadoquery.Delete;
try
insertStr:='insert into hj2 (fgf,ert,fdfd,dff,iuy,yuu,yuyy,MessageContent)'+
'values ('''+ FADOQuery.FieldByName('nnr').AsString+''','''+midstr(FADOQuery.FieldByName('nnr').AsString,3,length(FADOQuery.FieldByName('nnr').AsString))+''','+
''''+FADOQuery.FieldByName('hnr').Asstring+''','''+FADOQuery.FieldByName('bjr').Asstring+''','+
''''+FADOQuery.FieldByName('tuiff').AsString+''''+')';
FADOQuery.Close;
FADOQuery.SQL.Clear;
FADOQuery.SQL.Add(insertstr);
FADOQuery.ExecSQL;
except
form1.Memo1.Lines.Add('增加备份表出错了!');
end;
LeaveCriticalSection(CriticalMy);//退出临界段
finally
CoUninitialize;
FreeAndNil(FADOConnection);
FreeAndNil(FADOQuery);
FreeAndNil(FDataSource);
end;
sleep(1000);
end;
end;
如果用
for k:=1 to 100do
begin
mytread1:=mytread.Create(false);
end;
根本起不到我要的效果。请各位指点指点。怎么在一秒钟内同时启动100个MyTread线程呢?是在同一秒??或者在同一秒钟处理100条数据?只要能实现这种功能就行。
MyTread = class(TThread)
private
FADOConnection :TADOConnection;
FADOQuery :TADOQuery;
FDataSource :TDataSource;
protected
procedure Execute;
override;
end;
implementation
procedure MyTread.Execute;
var
i :integer;
DispMemo :string;
insertStr :string;
begin
freeonterminate:=true;
while not Terminateddo
begin
CoInitialize(0);
FADOConnection := TADOConnection.Create(nil);
FADOQuery := TADOQuery.Create(nil);
FDataSource := TDataSource.Create(nil);
try
FADOConnection.Close;
FADOConnection.ConnectionString := 'Provider=SQLOLEDB.1;Password=22;Persist Security Info=True;User ID=sa;Initial Catalog=sas;Data Source=HJ';
FADOConnection.Open;
FADOQuery.Connection := FADOConnection;
FDataSource.DataSet :=FADOQuery;
form1.Edit1.Text :=timetostr(time);
EnterCriticalSection(CriticalMy);//进入临界段
FADOQuery.Close;
FADOQuery.SQL.Clear;
FADOQuery.SQL.Add('select top 1 id,nnr,csr,gzr,bjr,tjr,');
FADOQuery.sql.Add('hnr,hbr,slr,dlr,xhr,fre,fgf,tuiff,');
FADOQuery.sql.Add(' from hj1 order by Priority desc');
FADOQuery.Open;
if FADOQuery.RecordCount =0 then
exit;
DispMemo:=' [ '+timetostr(time)+' ] '
+midstr(FADOQuery.FieldByName('nnr').AsString,3,length(FADOQuery.FieldByName('nnr').AsString))+' '
+FADOQuery.FieldByName('bjr').AsString+' '
+format('%x',[FADOQuery.FieldByName('hnr').asinteger])+' '
+FADOQuery.FieldByName('slr').AsString+' '
+FADOQuery.FieldByName('tuiff').AsString;
form1.Memo1.Lines.Add(DispMemo);
fadoquery.Delete;
try
insertStr:='insert into hj2 (fgf,ert,fdfd,dff,iuy,yuu,yuyy,MessageContent)'+
'values ('''+ FADOQuery.FieldByName('nnr').AsString+''','''+midstr(FADOQuery.FieldByName('nnr').AsString,3,length(FADOQuery.FieldByName('nnr').AsString))+''','+
''''+FADOQuery.FieldByName('hnr').Asstring+''','''+FADOQuery.FieldByName('bjr').Asstring+''','+
''''+FADOQuery.FieldByName('tuiff').AsString+''''+')';
FADOQuery.Close;
FADOQuery.SQL.Clear;
FADOQuery.SQL.Add(insertstr);
FADOQuery.ExecSQL;
except
form1.Memo1.Lines.Add('增加备份表出错了!');
end;
LeaveCriticalSection(CriticalMy);//退出临界段
finally
CoUninitialize;
FreeAndNil(FADOConnection);
FreeAndNil(FADOQuery);
FreeAndNil(FDataSource);
end;
sleep(1000);
end;
end;
如果用
for k:=1 to 100do
begin
mytread1:=mytread.Create(false);
end;
根本起不到我要的效果。请各位指点指点。怎么在一秒钟内同时启动100个MyTread线程呢?是在同一秒??或者在同一秒钟处理100条数据?只要能实现这种功能就行。