关于多线程的问题!在线等待!(100分)

  • 主题发起人 主题发起人 SamHunt
  • 开始时间 开始时间
S

SamHunt

Unregistered / Unconfirmed
GUEST, unregistred user!
我其实想做一个线程是操作数据库的!但想同步界面!却调试不成功!
请各位帮帮忙!

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, StdCtrls, Db, ADODB;

type
TForm1 = class(TForm)
Button1: TButton;
ProgressBar1: TProgressBar;
ADOQuery1: TADOQuery;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

type
TDBThread = class(TThread)
private
{ Private declarations }
FAdo:TAdoQuery;

procedure HookUpUI;

protected
procedure Execute;override;

public
{ Public declarations }
constructor Create;virtual;

end;


{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
TDBThread.Create;
end;

{ TDBThread }

constructor TDBThread.Create;
begin
inherited Create(True);
Resume;
Execute;
end;

procedure TDBThread.Execute;
var
i:Integer;
begin
//执行下面的那一句,就报错的!
FAdo.ConnectionString :='Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=IDSytem';

FAdo.SQL.Text:='Select * from Table1 ';
FAdo.Active:=false;
FAdo.Active:=true;

for i:=0 to 1000 do
begin
FAdo.Insert;
FAdo.FieldByName('Name').AsString:='aaa'+IntToStr(i);
FAdo.Post;
Synchronize(HookUpUI);
end;

end;

procedure TDBThread.HookUpUI;
begin
Form1.Label1.Caption:=FAdo.FieldByName('Name').AsString;
Form1.ProgressBar1.Position:=Form1.ProgressBar1.Position+1;
end;

end.
 
constructor TDBThread.Create;
begin
inherited Create(True);
FAdo:=TAdoQuery.Create(nil);//加入此句进行初始化.
Resume; //这里两甸有用吗.
Execute;//
end;

另外,重载destroy,在其中加入:
FAso.Free;
 
接受答案了.
 

Similar threads

I
回复
0
查看
698
import
I
I
回复
0
查看
644
import
I
I
回复
0
查看
787
import
I
I
回复
0
查看
684
import
I
I
回复
0
查看
721
import
I
后退
顶部