多线程数据处理问题!(100分)

  • 主题发起人 主题发起人 longchl
  • 开始时间 开始时间
L

longchl

Unregistered / Unconfirmed
GUEST, unregistred user!
我在delphi7中多线程数据处理时,建立了线程数组:
//处理线程数组;
QMncj:array of TDataWriteThread;
启动多线程:
//创建处理线程事例;
for i := 0 to FoSccs.m_nClXcs - 1do
begin
QMncj := TDataWriteThread.Create(FoSccs,lvwMncj,i);
end;

线程单元:
{**************************************************************************
//文件名:U_DataWriter.pas
unit U_DataWriter;
interface
uses
Classes,ADODB,DB, ComObj,Activex,idGlobal,SysUtils,Messages,windows, ComCtrls,
DBTables,PubClsUnit,PubUnit,U_DM;
type
TDataWriteThread = class(TThread)
private
//SQL模式数据连接;
FSessionMn:TSession;
FDatabaseMn:TDatabase;
//DBF模式数据连接;
FADOConnection:TADOConnection;
FADOQuery :TADOQuery;
FoSccs:TSccs;
FLvwMn:TListView;
FFlag:INteger;
protected
procedure Execute;
override;
procedure MncjClSQL;
procedure MncjClDBF;
procedure GetContent;
public
constructor Create(AoSccs:TSccs;lvwMn:TListView;i:Integer);
end;

implementation
{ DataWriteThread }
//功能:线程类的构造器
constructor TDataWriteThread.Create(AoSccs:TSccs;lvwMn:TListView;i:Integer);
begin
FoSccs := AoSccs;
FLvwMn := lvwMn;
FFlag := i;
//线程终止时自动删除对象
FreeOnTerminate := True;
inherited Create(False);
Priority := tpLower;
end;

//功能:执行线程的方法
//说明:每个线程创建单独的数据连接
procedure TDataWriteThread.Execute;
var
i:Integer;
sDBFPath,CStr:String;
slsParams:TStringList;
begin
//使用com对象必须要初始化
CoInitialize(nil);
try
if FoSccs.m_sDBType = 'SQL' then
//SQL Server DB
begin
slsParams := TStringList.Create;
slsParams.Append(C_sDBServerName + FoSccs.m_sSrvMc);
FSessionMn := TSession.Create(nil);
with FSessionMndo
begin
AutoSessionName := True;
Active := True;
ConfigMode := cmSession;
if IsAlias(C_sDBAliasName) then
DeleteAlias(C_sDBAliasName);
AddAlias(C_sDBAliasName, C_SQLDriver, slsParams);
end;

slsParams.Free;
FDataBaseMn := TDataBase.Create(nil);
if FoSccs.m_sSrvMc = '' then
exit;
with FDatabaseMndo
begin
SessionName := FSessionmn.SessionName;
LoginPrompt := False;
AliasName := C_sDBAliasName;
DatabaseName:= C_sDatabaseName;
Close;
Params.Clear;
Params.Add('User Name=' + FoSccs.m_sUserMc);
Params.Add('Password=' + FoSccs.m_sUserPW);
try
Open;
except
Exit;
end;
end;

Synchronize(MncjClSQL);
end
else
if FoSccs.m_sDBType = 'DBF' then
//DBF 数据库连接
begin
sDBFPath := GetFilePath(FoSccs.m_sWtk);
//DBF数据库的连接;
CStr := 'Provider=MSDASQL.1;Extended Properties="Driver={Microsoft Visual Foxpro Driver};SourceType=DBF;SourceDB=' + sDBFPath + '"';
// ';Mode=ReadWrite;
Persist Security Info=False';
FADOConnection := TADOConnection.Create(nil);
FADOConnection.ConnectionString := CStr;
FADOConnection.LoginPrompt := False;
try
FADOConnection.Open;
except
Exit;
end;

Synchronize(MncjClDBF);
end;
finally
CoUninitialize;
//必须使用
FreeAndNil(FDataBaseMn);
FreeAndNil(FSessionMn);
FreeAndNil(FADOConnection);
end;
end;

//功能: 模拟成交处理;
//说明: SQL模式
procedure TDataWriteThread.MncjClSQL;
var
nRec_num,nQty,nQty2:Integer;
sStr,sOwFlag,sStatus1,sQty2,sTeOrderNum:String;
dCjjg,dCjsl,dCjje:Double;
qryTemp,qryTemp2,qryTemp3:TQuery;
begin
qryTemp := TQuery.Create(nil);
qryTemp.SessionName := FDatabaseMn.SessionName;
qryTemp.DatabaseName := FDatabaseMn.DatabaseName;
qryTemp2 := TQuery.Create(nil);
qryTemp2.SessionName := FDatabaseMn.SessionName;
qryTemp2.DatabaseName := FDatabaseMn.DatabaseName;
qryTemp3 := TQuery.Create(nil);
qryTemp3.SessionName := FDatabaseMn.SessionName;
qryTemp3.DatabaseName := FDatabaseMn.DatabaseName;
FoSccs.m_nWtjl := 0;
FoSccs.m_nWtqr := 0;
FoSccs.m_nWtcd := 0;
FoSccs.m_nWtcj := 0;
while (FoSccs.m_nZT = 1)do
//如果处于运行状态,一直循环检测运行
begin
Synchronize(GetContent);
//1 获取参数数量
//1.1 获取实时委托数量;
qryTemp.Close;
qryTemp.SQL.Text := 'Select Count(rec_num) AS Wtjl From ' + FoSccs.m_sDBmc + '..' + FoSccs.m_sWtk +
' Where status = ''' + 'P''';
try
qryTemp.Open;
except
qryTemp.Free;
end;
FoSccs.m_nWtjl := qryTemp.FieldByName('Wtjl').AsInteger;
//1.2 获取委托确认数量;
qryTemp.Close;
qryTemp.SQL.Text := 'Select Count(rec_num)As Wtqr From ' + FoSccs.m_sDBmc + '..' + FoSccs.m_sWtQrk;
try
qryTemp.Open;
except
qryTemp.Free;
end;
FoSccs.m_nWtqr := qryTemp.FieldByName('Wtqr').AsInteger;
//1.3 获取撤单数量;
qryTemp.Close;
qryTemp.SQL.Text := 'Select Count(rec_num) AS Wtcd From ' + FoSccs.m_sDBmc + '..' + FoSccs.m_sWtk +
' Where status = ''' + 'P''' + ' And owflag = ''' + 'WTH''';
try
qryTemp.Open;
except
qryTemp.Free;
end;
FoSccs.m_nWtcd := qryTemp.FieldByName('Wtcd').AsInteger;
//1.4 获取实时成交数量;
qryTemp.Close;
qryTemp.SQL.Text := 'Select Count(cjbh)As Wtcj From ' + FoSccs.m_sDBmc + '..' + FoSccs.m_sSsCjk;
try
qryTemp.Open;
except
qryTemp.Free;
end;
FoSccs.m_nWtcj := qryTemp.FieldByName('Wtcj').AsInteger;
//2 委托处理
//2.1 获取委托记录数据;
//显示第一条记录;
qryTemp.Close;
qryTemp.SQL.Text := 'Select top 1 * From ' + FoSccs.m_sDBmc + '..' + FoSccs.m_sWtk +
' Where status = ''' + 'R' + ''' Or status = ''' + 'r''';
try
qryTemp.Open;
except
qryTemp.Free;
end;
if not qryTemp.Eof then
// if - 2.1
begin
nRec_Num := qryTemp.FieldByName('rec_num').AsInteger;
TryStrToInt(qryTemp.FieldByName('qty').AsString,nQty);
sOwFlag := qryTemp.FieldByName('owflag').AsString;
if sOwFlag = 'LPT' then
begin
sStatus1 := 'O';
nQty2 := 0;
sQty2 := '';
sTeOrderNum := IntToStr(nRec_Num);
end
else
if sOwFlag = 'WTH' then
begin
sStatus1 := 'W';
nQty2 := nQty;
sQty2 := IntToStr(nQty2);
sTeOrderNum := '';
end
else
begin
sStatus1 := '';
nQty2 := 0;
sQty2 := '';
sTeOrderNum := '';
end;

//2.2 修改委托库的标志位;
qryTemp2.Close;
qryTemp2.SQL.Text := 'Update ' + FoSccs.m_sDBmc + '..' + FoSccs.m_sWtk +
' Set status = ''' + 'P' + '''' +
',TFlag = ' + IntToStr(FFlag) +
' Where rec_num = ' + IntToStr(nRec_Num);
try
qryTemp2.ExecSQL;
except
qryTemp2.Free;
end;

//2.3 信息写入委托确认表;
//例子:insert aordwth2 values('1','20071006','22:40:05','om65000032','A601597072','600372','B','8.0','200','O','','','P','1','LPT','','',0)
sStr := 'Insert Into ' + FoSccs.m_sDBmc + '..' + FoSccs.m_sWtQrk + '(rec_num,date,time,reff,acc,stock,bs,price,qty,status,qty2,status1,teordernum,owflag,ordrec,firmid,checkord) Values(' +
IntToStr(nRec_Num) +
',''' + qryTemp.FieldByName('Date').AsString + '''' +
',''' + qryTemp.FieldByName('Time').AsString + '''' +
',''' + qryTemp.FieldByName('Reff').AsString + '''' +
',''' + qryTemp.FieldByName('Acc').AsString + '''' +
',''' + qryTemp.FieldByName('Stock').AsString + '''' +
',''' + qryTemp.FieldByName('Bs').AsString + '''' +
',''' + qryTemp.FieldByName('Price').AsString + '''' +
',''' + IntToStr(nQty) + '''' +
',''' + sStatus1 + '''' +
',''' + sQty2 + '''' +
',''' + 'P''' +
',''' + sTeOrderNum + '''' +
',''' + sOwFlag + '''' +
',''' + qryTemp.FieldByName('ordrec').AsString + '''' +
',''' + qryTemp.FieldByName('firmid').AsString + '''' +
',0' +
')';
qryTemp2.Close;
qryTemp2.SQL.Text := sStr;
try
qryTemp2.ExecSQL;
except
qryTemp2.Free;
end;

//2.4 信息写入成交回报表;
//按比例成交,比例大于0;
//比例为0则不成交;
if (FoSccs.m_dCjms > 0) And (sOwFlag <> 'WTH') then
begin
dCjsl := RoundF(nQty * FoSccs.m_dCjms / 100,0);
//成交数量
TryStrToFloat(qryTemp.FieldByName('Price').AsString,dCjjg);
dCjje := RoundF(dCjjg * dCjsl,-2);
//例子:insert acjhb values('A601597072','','20071006','1','61011','200','200','600372','224005','224005','8.0','1600.000','om65000032','B','')
sStr := 'Insert Into ' + FoSccs.m_sDBmc + '..' + FoSccs.m_sSsCjk + '(gddm,gdxm,bcrq,cjbh,gsdm,cjsl,bcye,zqdm,sbsj,cjsj,cjjg,cjje,sqbh,bs,mjbh) Values(' +
' ''' + qryTemp.FieldByName('Acc').AsString + '''' + //股东代码,证券账户
',''' + '' + '''' + //股东姓名
',''' + qryTemp.FieldByName('Date').AsString + '''' + //成交日期
',''' + IntToStr(nRec_Num) + '''' + //成交编号
',''' + FoSccs.m_sGsdm + '''' + //席位号
',''' + FormatFloat('0',dCjsl) + '''' + //成交数量
',''' + '0' + '''' + //本次余额,全部设置为0
',''' + qryTemp.FieldByName('Stock').AsString + '''' + //证券代码
',''' + FormatDateTime('HHMMSS',Now) + '''' + //申报时间
',''' + FormatDateTime('HHMMSS',Now) + '''' + //成交时间
',''' + qryTemp.FieldByName('Price').AsString + '''' + //成交价格
',''' + FormatFloat('0.00',dCjje) + '''' + //成交金额
',''' + qryTemp.FieldByName('Reff').AsString + '''' + //会员内部订单号
',''' + qryTemp.FieldByName('Bs').AsString + '''' + //买卖方向
',''' + ' ''' + //操作人员编号
')';
qryTemp2.Close;
qryTemp2.SQL.Text := sStr;
try
qryTemp2.ExecSQL;
except
qryTemp2.Free;
end;
end;
end;
//end of if - 2.1
end;
//end of - while
qryTemp.Free;
qryTemp2.Free;
qryTemp3.Free;
end;

//功能: 模拟成交处理;
//说明: DBF模式
procedure TDataWriteThread.MncjClDBF;
var
nCjhm:Integer;
sWtDbf,sWtqrDbf,sHbDbf:String;
sWthtXh,sWtYwlb,sHbCjhm:String;
sStr:String;
dWtWtjg,dWtWtsl,dCjsl:Double;
qryTemp,qryTemp2,qryTemp3:TADOQuery;
begin
qryTemp := TADOQuery.Create(nil);
qryTemp.Connection := FADOConnection;
qryTemp2 := TADOQuery.Create(nil);
qryTemp2.Connection := FADOConnection;
qryTemp3 := TADOQuery.Create(nil);
qryTemp3.Connection := FADOConnection;
sWtDbf := GetDbfName(FoSccs.m_sWtk);
sWtqrDbf:= GetDbfName(FoSccs.m_sWtQrk);
sHbDbf := GetDbfName(FoSccs.m_sSSCjk);
if (sWtDbf = '') Or (sHbDbf = '') then
Exit;
FoSccs.m_nWtjl := 0;
FoSccs.m_nWtqr := 0;
FoSccs.m_nWtcd := 0;
FoSccs.m_nWtcj := 0;
while (FoSccs.m_nZT = 1)do
//如果处于运行状态,一直循环检测运行
begin
Synchronize(GetContent);
//1 获取参数数量
//1.1 获取实时委托数量;
qryTemp.Close;
qryTemp.SQL.Text := 'Select Count(WthtXh) AS Wtjl From ' + sWtDbf +
' Where WtClbz = ''' + '1''';
try
qryTemp.Open;
except
qryTemp.Free;
end;
FoSccs.m_nWtjl := qryTemp.FieldByName('Wtjl').AsInteger;
//1.2 获取委托确认数量;
qryTemp.Close;
qryTemp.SQL.Text := 'Select Count(WthtXh) AS Wtqr From ' + sWtqrDbf +
' Where WtClbz = ''' + '1''';
try
qryTemp.Open;
except
qryTemp.Free;
end;
FoSccs.m_nWtqr := qryTemp.FieldByName('Wtqr').AsInteger;
//1.3 获取撤单数量;
qryTemp.Close;
qryTemp.SQL.Text := 'Select Count(WthtXh) AS Wtcd From ' + sWtDbf +
' Where WtClbz = ''' + '1''' + ' And WtYwlb = ''' + '0C''';
try
qryTemp.Open;
except
qryTemp.Free;
end;
FoSccs.m_nWtcd := qryTemp.FieldByName('Wtcd').AsInteger;
//1.4 获取实时成交数量;
qryTemp.Close;
qryTemp.SQL.Text := 'Select Count(HbCjhm)As Wtcj From ' + sHbDbf;
try
qryTemp.Open;
except
qryTemp.Free;
end;
FoSccs.m_nWtcj := qryTemp.FieldByName('Wtcj').AsInteger;
//2 委托处理
//2.1 获取委托记录数据;
qryTemp.Close;
qryTemp.SQL.Text := 'Select top 1 * From ' + sWtDbf + ' Where WtClbz = ''' + 'z' + ''' Or WtClbz = ''' + 'Z''';
try
qryTemp.Open;
except
qryTemp.Free;
end;
if not qryTemp.Eof then
// if - 2.1
begin
sWthtXh := qryTemp.FieldByName('WthtXh').AsString;
dWtWtsl := qryTemp.FieldByName('WtWtsl').AsInteger;
dWtWtjg := qryTemp.FieldByName('WtWtjg').AsFloat;
sWtYwlb := qryTemp.FieldByName('WtYwlb').AsString;
//2.2 修改委托库的标志位;
qryTemp2.Close;
qryTemp2.SQL.Text := 'Update ' + sWtDbf + ' Set WtClbz = ''' + '1' + '''' +
' Where WthtXh = ''' + sWthtXh + '''';
try
qryTemp2.ExecSQL;
except
qryTemp2.Free;
end;

//2.3 撤单的处理
//不成功撤单 , 成交数量为0,否则为撤单数量 * (-1)
if sWtYwlb = '0C' then
dCjsl := 0;
//2.4 信息写入成交回报表;
//按比例成交,比例大于0;
//比例为0则不成交;
if FoSccs.m_dCjms > 0 then
begin
dCjsl := Int(dWtWtsl * FoSccs.m_dCjms / 100);
//成交数量
sHbCjhm := '00000001';
DateSeparator := '/';
ShortDateFormat:= 'yyyy/mm/dd';
//获取成交编号
qryTemp2.Close;
qryTemp2.SQL.Text := 'Select Max(HbCjhm) As Cjhm From ' + sHbDbf;
try
qryTemp2.Open;
except
qryTemp2.Free;
end;
if not qryTemp2.Eof then
nCjhm := qryTemp2.FieldByName('Cjhm').AsInteger + 1
else
nCjhm := 1;
sHbCjhm := GetHbCjhm(nCjhm);
sStr := 'Insert Into ' + sHbDbf +
'(HbCjhm,HbZqdm,HbHtxh,HbGddm,HbCjsl,HbCjjg,HbDfxw,HbDfgd,HbCjsj,HbCjrq,HbYwlb,HbCdyy,HbMark,HbBybz) Values(' +
' ''' + sHbCjhm + '''' + //成交号码
',''' + qryTemp.FieldByName('WtZqdm').AsString + '''' + //证券代码
',''' + sWtHtxh + '''' + //合同序号
',''' + qryTemp.FieldByName('WtGddm').AsString + '''' + //证券账户
',' + FloatToStr(dCjsl) + //成交数量
',' + FloatToStr(dWtWtjg) + //成交价格
',''' + '''' + //对方席位
',''' + '''' + //对方股东
',''' + FormatDateTime('HH:MM:SS',Now) + '''' + //成交时间
',''' + FormatDateTime('YYYY-MM-DD',Date) + '''' + //成交日期
',''' + qryTemp.FieldByName('WtYwlb').AsString + '''' + //业务类别
',''' + '''' + //撤单原因
',''' + '''' + //备用字段
',''' + '''' + //备用标志
')';
qryTemp2.Close;
qryTemp2.SQL.Text := sStr;
try
qryTemp2.ExecSQL;
except
qryTemp2.Free;
end;
end;

end;
//end of if - 2.1
end;
//end of - while
qryTemp.Free;
qryTemp2.Free;
qryTemp3.Free;
end;

//功能:处理显示结果信息;
procedure TDataWriteThread.GetContent;
var
oItem:TListItem;
begin
oItem := FLvwMn.Selected;
oItem.SubItems[0] := IntToStr(FoSccs.m_nWtjl);
oItem.SubItems[1] := IntToStr(FoSccs.m_nWtqr);
oItem.SubItems[2] := IntToStr(FoSccs.m_nWtcd);
oItem.SubItems[3] := IntToStr(FoSccs.m_nWtcj);
FLvwMn.Refresh;
end;

end.
 
问题已经解决,多谢各位的关心!
问题主要是:非VCL事件进行了同步处理。
 
呵呵,派生 thread 时 borland 在 pas 那段注释一定要看清楚啊。
 
多谢提醒!
 
后退
顶部