把 一少的程序稍加修改就可以多个线程处理一个文件了
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
Tmythread = class(Tthread)
private
FnBeginPos:longint;
FnEndPos:longint;
FTTSringlist:TStrings;
protected
mFile: string;
constructor create(s: Tstrings;nBeginPos,nEndPos:Integer); overload;
procedure execute; override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
end;
{ Tmythread }
constructor Tmythread.create(s: Tstrings;nBeginPos,nEndPos:Integer);
begin
FTTSringlist:=TStringlist.create;
FTTSringlist.clear;
FTTSringlist.Assign(s);
FnBeginPos:=nBeginPos;
FnEndPos:=nEndPos;
inherited create(false);
end;
procedure Tmythread.execute;
var
i: integer;
mRecord: string;
begin
inherited;
if nBeginPos>=FTTSringlist.count then
begin
FTTSringlist.free;
exit;
end;
if nEndPos>FTTSringlist.count then nEndPos:=FTTSringlist.count;
for i := nBeginPos to nEndPos-1 do
begin
mRecord := strlst; //一行一行读出文本中的记录
{ 写入数据库的语句 }
end;
FTTSringlist.free;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
mthread1,mthread2: TMythread;
strlst: TStringlist;
begin
strlst := TStringList.Create;
strlst.LoadFromFile('c:/11.txt');
mthread1 := TMythread.create(strlst,0,10000);
mthread2 := TMythread.create(strlst,10000,strlst.count);
//...实现线程
strlst.Free;
end;
end.