300分求ADO多线程操作的源码(300分)

  • 主题发起人 主题发起人 hpbj
  • 开始时间 开始时间
H

hpbj

Unregistered / Unconfirmed
GUEST, unregistred user!
最近编写多线程操作的程序,一直有些问题。
谁有这方面的例子,300分送上。
hpbj@sina.com
 
如果有谁发也给我一份,200分奉上。hanjiwei@sjyh.net
 
unit Mdatabas;
interface
uses
Classes, ADODB;
type
TDatabaseThread = class(TThread)
private
FADODataSet:TADODataSet;
FADOQuery:TADOQuery;
FADOCommand:TADOCommand;
SQLString:string;
protected
procedure Execute;
override;
public
constructor Create(ADO:TADODataSet;S:String);
overload;
constructor Create(ADO:TADOQuery;S:String);
overload;
constructor Create(ADO:TADOCommand;S:String);
overload;
end;

implementation
{ TDatabaseThread }
constructor TDatabaseThread.Create(ADO:TADODataSet;S:String);
begin
FADODataSet:=ADO;
SQLString:=S;
inherited Create(False);
end;

constructor TDatabaseThread.Create(ADO:TADOQuery;S:String);
begin
FADOQuery:=ADO;
SQLString:=S;
inherited Create(False);
end;

constructor TDatabaseThread.Create(ADO:TADOCommand;S:String);
begin
FADOCommand:=ADO;
SQLString:=S;
inherited Create(False);
end;

procedure TDatabaseThread.Execute;
begin
FreeOnTerminate:=True;
if FADODataSet<>nil then
begin
FADODataSet.Close;
FADODataSet.CommandType:=cmdText;
FADODataSet.CommandText:=SQLString;
FADODataSet.Open;
end;
if FADOQuery<>nil then
begin
FADOQuery.Close;
FADOQuery.SQL.Clear;
FADOQuery.SQL.Add(SQLString);
FADOQuery.ExecSQL;
end;
if FADOCommand<>nil then
begin
FADOCommand.CommandText:=SQLString;
FADOCommand.Execute;
end;
if Terminated then
exit;
end;

end.
 
金风细雨:
我需要一套源程序:
比如点击主窗体的一个按钮,程序将进行多线程的查询操作。
你的例子不能体现这些。
谢谢。
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1076676
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel;
TrackBar1: TTrackBar;
TrackBar2: TTrackBar;
TrackBar3: TTrackBar;
TrackBar4: TTrackBar;
PBar1: TProgressBar;
PBar2: TProgressBar;
PBar3: TProgressBar;
PBar4: TProgressBar;
Button1: TButton;
Button2: TButton;
Button4: TButton;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure TrackBar1Change(Sender: TObject);
procedure TrackBar2Change(Sender: TObject);
procedure TrackBar3Change(Sender: TObject);
procedure TrackBar4Change(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
uses unit2;
var
thread:array[1..4] of mymath;
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var a:boolean;
begin
//析构线程
thread[1].destroy;
thread[2].destroy;
thread[3].destroy;
thread[4].destroy;
//重新建立线程
thread[1]:=mymath.create;
thread[2]:=mymath.create;
thread[3]:=mymath.create;
thread[4]:=mymath.create;
thread[1].Pbarth:=Pbar1;
thread[2].Pbarth:=Pbar2;
thread[3].Pbarth:=Pbar3;
thread[4].Pbarth:=Pbar4;
//初始化
Pbar1.position:=0;
Pbar2.position:=0;
Pbar3.position:=0;
Pbar4.position:=0;
//tthreadpriority(i)是个函数,给出相应优先级的文字
thread[1].priority:=tthreadpriority(trackbar1.position);
thread[2].priority:=tthreadpriority(trackbar2.position);
thread[3].priority:=tthreadpriority(trackbar3.position);
thread[4].priority:=tthreadpriority(trackbar4.position);
thread[1].Resume;
thread[2].Resume;
thread[3].Resume;
thread[4].Resume;

end;

procedure TForm1.Button2Click(Sender: TObject);
begin

//线程挂起
thread[1].suspend;
thread[2].suspend;
thread[3].suspend;
thread[4].suspend;
end;

procedure TForm1.TrackBar1Change(Sender: TObject);
begin
thread[1].priority:=tthreadpriority(trackbar1.position);
end;

procedure TForm1.TrackBar2Change(Sender: TObject);
begin
thread[2].priority:=tthreadpriority(trackbar2.position);
end;

procedure TForm1.TrackBar3Change(Sender: TObject);
begin
thread[3].priority:=tthreadpriority(trackbar3.position);
end;

procedure TForm1.TrackBar4Change(Sender: TObject);
begin
thread[4].priority:=tthreadpriority(trackbar4.position);
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
Application.Terminate;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
//同一个线程单元,可以同时构建多个线程
thread[1]:=mymath.create;
thread[2]:=mymath.create;
thread[3]:=mymath.create;
thread[4]:=mymath.create;
end;

end.
 
下面是我写的一小段,要点是每个线程都有自己独立的AdoConnection:
unit DataWriter;
interface
uses
Classes,ADODB,ComObj,Activex,idGlobal,SysUtils,Messages,windows;

type
DataWriteThread = class(TThread)
private
FADOConnection:TADOConnection;
FADOQuery:TADOQuery;
FContentList:TStringList;
FStartIndex,FEndIndex:Integer;
FIndex:Integer;
FContent:string;
protected
procedure Execute;
override;
procedure GetContent;
procedure PostEndThreadMessage;
public
constructor Create(StartIndex,EndIndex:Integer);
end;

implementation
uses mf;
{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure DataWriteThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ DataWriteThread }

constructor DataWriteThread.Create(StartIndex,EndIndex:Integer);
begin
FStartIndex := StartIndex;
FEndIndex := EndIndex;
FreeOnTerminate := True;
inherited Create(False);
end;

procedure DataWriteThread.Execute;
var
i:Integer;
begin
CoInitialize(0);
FADOConnection := TADOConnection.Create(nil);
FADOQuery := TADOQuery.Create(nil);
try
FADOConnection.ConnectionString := 'Provider=SQLOLEDB.1;Password=123;Persist Security Info=True;User ID=sa;Initial Catalog=pubs;Data Source=zhanghl';
FADOConnection.Open;
FADOQuery.Connection := FADOConnection;
FIndex := FStartIndex;
FADOQuery.CacheSize := 800;
FADOQuery.LockType := ltBatchOptimistic;
FADOQuery.Close;
FADOQuery.SQL.Text := 'select top 1 tm from lw';
FADOQUery.Open;
for i := FStartIndex to FEndIndexdo
begin
Synchronize(GetContent);
try
FADOQuery.Append;
FADOQuery.FieldByName('tm').AsString := FContent;
FADOQuery.Post;

except
end;
end;
FADOQuery.UpdateBatch(arAll);
finally
CoUninitialize;
FreeAndNil(FADOConnection);
FreeAndNil(FADOQuery);
Synchronize(PostEndThreadMessage);
end;
end;

procedure DataWriteThread.GetContent;
begin
FContent := MainForm.ListBox.Items[FIndex];
MainForm.NowDo(FIndex);
FIndex := FIndex + 1;
end;

procedure DataWriteThread.PostEndThreadMessage;
begin
PostMessage(MainForm.Handle,WM_DataWriterEnd,0,0);
end;

end.
 
看看大富翁论坛离线浏览器的源代码吧
 
后退
顶部