全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2786
推到 Plurk!
推到 Facebook!

請教Multi Thread的問題

缺席
foxelf
初階會員


發表:9
回覆:42
積分:30
註冊:2003-03-05

發送簡訊給我
#1 引用回覆 回覆 發表時間:2007-12-27 09:48:42 IP:211.78.xxx.xxx 訂閱
各位大大  小弟做了一支Multi Thread的程式,想要同時異動同一個料庫中數個Table,每個Thread會針對其中一個Table作異動,並將Table的總數及現在執行的到第幾筆數的資料秀到FORM上的Label,但是FORM接收到的只有兩個Label有動作,可能是判斷要回傳到哪個Label那地方有問題,但是又不知要怎麼做,程式有問題的地方,請各位大大指教
------------------------------------------------------------------------------------------------------------------------------------------------------------
小弟又重新改了一些CODE,線在MAINFORM上的LABEL可以有數字再跑了,但是一些沒有搜收尋到資料所指定的LABEL也跟著跑數字而且是有資料的TABLE的數字
-------------------------------------------------------------------------------------------------------------------------------------------------------------
各位大大 現在已經可以依照各對應的LABEL去顯示數字了,可是在UPDATE的時候沒有資料的ADOQUERY居然也會去塞入別的TABLE的SQL語法而造成SQL的ERROR,我想要將那個不需要動作的THREAD給FREE了,但是還是會有一樣的SQL錯誤
-------------------------------------------------------------------------------------------------------------------------------------------------------------

好像不同的THREAD內的ADOQuery會抓到其他THREAD的SQL資料而造成的
-------------------------------------------------------------------------------------------------------------------------------------------------------------
小弟 已經找出問題點了 原來是給參數的時候所放的位置不同,在QueryOpen中給值就不會錯了



[code delphi]
unit uExecTherad;
interface
uses
Classes, ADODB, DBCtrls, DB, SysUtils, StdCtrls, Forms, ActiveX ;
type
ExecLawSQL = class(TThread)
private
{ Private declarations }
aQuery:TADOQuery;
uQuery:TADOQuery;
aConn:TADOConnection;
aSour:TDataSource;
inInt:Integer ;
procedure QueryOpen ;
protected
Memo:TDBMemo;
RCount:Integer ;
TCount:Integer ;
SetList:TStringList ;
tLabel:TLabel;
rLabel:TLabel;
procedure Execute; override;
procedure ExceSQLData ;virtual;
public
constructor Create(sList:TStringList;Conn:TADOConnection;Query,UpdQuery:TADOQuery;dSour:TDataSource;
t_Label,r_Label:TLabel;dMemo:TDBMemo);
end;
implementation
uses uMainThread ;
threadvar
iSetList :Integer ;
{ 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 ExecLawSQL.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }
{ ExecLawSQL }
constructor ExecLawSQL.Create(sList:TStringList;Conn:TADOConnection;Query,UpdQuery:TADOQuery;dSour:TDataSource;
t_Label,r_Label:TLabel;dMemo:TDBMemo);
begin
SetList := sList ;
TCount := 0 ;
RCount := 0 ;
aConn := Conn ;
aQuery := Query ;
uQuery := UpdQuery ;
aSour := dSour ;
Memo := dMemo ;
tLabel := t_Label ;
rLabel := r_Label ;
iSetList := -1 ;
inInt := StrToInt(SetList[5]);
inherited Create(False);
QueryOpen ;
end ;
procedure ExecLawSQL.Execute;
begin
{ Place thread code here }
if Terminated then
begin
FreeAndNil(Self) ;
end ;
FreeOnTerminate := True ;
ExceSQLData ;
end;
procedure ExecLawSQL.QueryOpen ;
var isList:Integer ;
begin
// CoInitialize(nil);
aConn.LoginPrompt := False ;
aConn.ConnectionString := SetList[3];
aQuery.Close ;
aQuery.Connection := aConn ;
uQuery.Connection := aConn ;
Memo.DataField := SetList[0] 'CONTENT';
aQuery.SQL.Clear ;
aQuery.SQL.Add(SetList[2]);
aQuery.Open ;
if aQuery.IsEmpty then
begin
tLabel.Caption := '0';
rLabel.Caption := '0';
Terminate ;
end
else
begin
tLabel.Caption := IntToStr(aQuery.RecordCount);
aSour.DataSet := aQuery ;
Memo.DataSource := aSour ;
end ;
end ;
procedure ExecLawSQL.ExceSQLData ;
var sSource :TStringList ;
sContent:TStringList ;
Bool :Boolean ;
iTable:Integer ;
i,eRecord :Integer ;
GenLine :String ;
maStr, maStr1, maStr2 :String ;
StrWhere :String ;
begin
sSource := TStringList.Create ;
sContent := TStringList.Create ;
maStr := SetList[1];
iTable := StrToInt(SetList[5]);
StrWhere := SetList[6];
maStr1 := SetList[7];
maStr2 := SetList[8];
Bool := False ;
eRecord := 0 ;
if iSetList = -1 then
iSetList := inInt ;
While not aQuery.Eof do
begin
for i := 0 to Memo.Lines.Count -1 do
begin
if (POS(maStr,Memo.Lines.Strings[i]) > 0)or(POS(maStr1,Memo.Lines.Strings[i]) > 0) then
begin
if not Bool then Bool := True ;
if Pos(']]',Memo.Lines.Strings[i]) = 0 then
begin
sSource.Add(Copy(Memo.Lines.Strings[i],Length(maStr2) 1,Length(Memo.Lines.Strings[i])))
end
else
sContent.Add(Memo.Lines.Strings[i]) ;
end
else
begin
if (Bool) And (Pos(']]',Memo.Lines.Strings[i]) = 0) then
begin
sSource.Add(Memo.Lines.Strings[i]) ;
end
else
begin
Bool := False ;
sContent.Add(Memo.Lines.Strings[i]) ;
end ;
end ;
end ;
if not aQuery.IsEmpty then
begin
uQuery.Close ;
uQuery.SQL.Clear ;
uQuery.SQL.Add('UpDate ' SetList[4] ' Set ' SetList[0] 'SOURCE= :SOURCE,EDITDATE =:DATE,');
uQuery.SQL.Add('EUSER=''WUBACK'',' SetList[0] 'CONTENT=:CONTENT Where ' StrWhere);
if aQuery.FieldByName(SetList[0] 'SOURCE').Value <> '' then
sSource.Add(aQuery.FieldByName(SetList[0] 'SOURCE').Value) ;
uQuery.Parameters[0].DataType := ftBlob ;
uQuery.Parameters[2].DataType := ftBlob ;
uQuery.Parameters[0].Value := sSource.Text ;
uQuery.Parameters[1].Value := Now ;
uQuery.Parameters[2].Value := sContent.Text ;
uQuery.ExecSQL ;
end ;

sSource.Clear ;
sContent.Clear ;
RCount := RCount 1 ;
rLabel.Caption := IntToStr(RCount);
Application.ProcessMessages ;
aQuery.Next ;
end ;
// CoUninitialize;
end ;
end.
[/code]
我將UPDATE DB的部份貼出

[code delphi]
//SetList是我Create Thread的時候傳入的TStringList (SQL, 欄位..........)

if not aQuery.IsEmpty then
begin
uQuery.Close ;
uQuery.SQL.Clear ;
uQuery.SQL.Add('UpDate ' SetList[4] ' Set ' SetList[0] 'SOURCE= :SOURCE,EDITDATE =:DATE,');
uQuery.SQL.Add('EUSER=''AUser'',' SetList[0] 'CONTENT=:CONTENT Where ' CutField(StrWhere));
if aQuery.FieldByName(SetList[0] 'SOURCE').Value <> '' then <<-----SetList[0] 'SOURCE'與aQuery內的欄位不同
sSource.Add(aQuery.FieldByName(SetList[0] 'SOURCE').Value) ;
uQuery.Parameters[0].DataType := ftBlob ;
uQuery.Parameters[2].DataType := ftBlob ;
uQuery.Parameters[0].Value := sSource.Text ;
uQuery.Parameters[1].Value := Now ;
uQuery.Parameters[2].Value := sContent.Text ;
uQuery.ExecSQL ;
end ;

[/code]
編輯記錄
foxelf 重新編輯於 2007-12-27 16:24:59, 註解 修改後的CODE‧
foxelf 重新編輯於 2007-12-27 18:07:21, 註解 無‧
foxelf 重新編輯於 2007-12-28 10:24:13, 註解 無‧
foxelf 重新編輯於 2007-12-28 10:26:34, 註解 無‧
foxelf 重新編輯於 2007-12-31 10:56:22, 註解 無‧
系統時間:2024-04-19 15:41:47
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!