B
bylove
Unregistered / Unconfirmed
GUEST, unregistred user!
里面出问题的地方标记:为什么这里出错了? ,请高手指点:
主界面里面是:
procedure TForm1.Button4Click(Sender: TObject);
begin
with TForm2.Create(self)do
begin
try
ShowModal;
finally
free;
end;
end;
end;
在子窗口里面有个进度条,有个按钮,按钮的内容是执行读取一个文件逐行处理
procedure TForm2.Button2Click(Sender: TObject);
begin
Memo1.Clear;
//创建导入线程,导入数据库
FThread := TReadPhoneListThread.Create(Trim(edit1.Text), 1);
FThread.Resume;
end;
//下面是线程的方法
constructor TReadPhoneListThread.Create(AFileName: string;
AClass: integer);
begin
FFileName := AFileName;
inherited Create(False);
//false表示创建后先挂起
end;
procedure TReadPhoneListThread.Execute;
var
vFile: TextFile;
S, vName, vPhone: string;
function GetLineCount: integer;
var
i: Integer;
s: string;
begin
AssignFile(vFile, FFileName);
Reset(vFile);
i := 0;
while not Eof(vFile)do
begin
Readln(vFile, s);
Inc(i);
end;
CloseFile(vFile);
Result := i;
end;
begin
FreeOnTerminate := True;
//标示线程结束时自动释放你的TThread对象
if fileexists(FFileName) then
begin
//取得文件总行数
FCount := GetLineCount;
//把总行数信息送到主界面
Synchronize(SendCountToGauge);
//SendCountToGauge是线程中定义的把线程内部参数值传给窗口的函数
//重新打开文件
FCurrent := 0;
Synchronize(SendCurrToGauge);
AssignFile(vFile, FFileName);
Reset(vFile);
//打开
while not Eof(vFile)do
//开始循环处理
begin
S := '';
FName:='';
FPhone :='';
ReadLn(vFile, S);
//读一行信息;
if Length(Trim(s)) <> 0 then
begin
//开始处理,解析名称和手机号
FName := Copy(s, 1, Pos(#9, s) - 1);
//取得姓名
FPhone := Copy(s, Pos(#9, s) + 1, Length(s) - Pos(#9, s) + 1);
//取得号码
Synchronize(SendInfotoMemo);
//插入数据库;
//刷新进度
inc(FCurrent);
Synchronize(SendCurrToGauge);
end;
Sleep(10);
end;
//通知主窗体完成了
Synchronize(SendFinishedToInterface);
end;
end;
procedure TReadPhoneListThread.SendCountToGauge;
begin
Form2.ProgressBar.Max := FCount;
//为什么这里出错了?
Form2.ProgressBar.Update;
end;
procedure TReadPhoneListThread.SendCurrToGauge;
begin
Form2.ProgressBar.Position := FCurrent;
// 为什么这里出错了?
Form2.ProgressBar.Update;
end;
procedure TReadPhoneListThread.SendFinishedToInterface;
begin
{}
end;
procedure TReadPhoneListThread.SendInfotoMemo;
begin
Form2.Memo1.Lines.Add(FName+'_'+FPhone);
//为什么这里出错了?
Form2.Memo1.Update;
end;
主界面里面是:
procedure TForm1.Button4Click(Sender: TObject);
begin
with TForm2.Create(self)do
begin
try
ShowModal;
finally
free;
end;
end;
end;
在子窗口里面有个进度条,有个按钮,按钮的内容是执行读取一个文件逐行处理
procedure TForm2.Button2Click(Sender: TObject);
begin
Memo1.Clear;
//创建导入线程,导入数据库
FThread := TReadPhoneListThread.Create(Trim(edit1.Text), 1);
FThread.Resume;
end;
//下面是线程的方法
constructor TReadPhoneListThread.Create(AFileName: string;
AClass: integer);
begin
FFileName := AFileName;
inherited Create(False);
//false表示创建后先挂起
end;
procedure TReadPhoneListThread.Execute;
var
vFile: TextFile;
S, vName, vPhone: string;
function GetLineCount: integer;
var
i: Integer;
s: string;
begin
AssignFile(vFile, FFileName);
Reset(vFile);
i := 0;
while not Eof(vFile)do
begin
Readln(vFile, s);
Inc(i);
end;
CloseFile(vFile);
Result := i;
end;
begin
FreeOnTerminate := True;
//标示线程结束时自动释放你的TThread对象
if fileexists(FFileName) then
begin
//取得文件总行数
FCount := GetLineCount;
//把总行数信息送到主界面
Synchronize(SendCountToGauge);
//SendCountToGauge是线程中定义的把线程内部参数值传给窗口的函数
//重新打开文件
FCurrent := 0;
Synchronize(SendCurrToGauge);
AssignFile(vFile, FFileName);
Reset(vFile);
//打开
while not Eof(vFile)do
//开始循环处理
begin
S := '';
FName:='';
FPhone :='';
ReadLn(vFile, S);
//读一行信息;
if Length(Trim(s)) <> 0 then
begin
//开始处理,解析名称和手机号
FName := Copy(s, 1, Pos(#9, s) - 1);
//取得姓名
FPhone := Copy(s, Pos(#9, s) + 1, Length(s) - Pos(#9, s) + 1);
//取得号码
Synchronize(SendInfotoMemo);
//插入数据库;
//刷新进度
inc(FCurrent);
Synchronize(SendCurrToGauge);
end;
Sleep(10);
end;
//通知主窗体完成了
Synchronize(SendFinishedToInterface);
end;
end;
procedure TReadPhoneListThread.SendCountToGauge;
begin
Form2.ProgressBar.Max := FCount;
//为什么这里出错了?
Form2.ProgressBar.Update;
end;
procedure TReadPhoneListThread.SendCurrToGauge;
begin
Form2.ProgressBar.Position := FCurrent;
// 为什么这里出错了?
Form2.ProgressBar.Update;
end;
procedure TReadPhoneListThread.SendFinishedToInterface;
begin
{}
end;
procedure TReadPhoneListThread.SendInfotoMemo;
begin
Form2.Memo1.Lines.Add(FName+'_'+FPhone);
//为什么这里出错了?
Form2.Memo1.Update;
end;