L
lwbcool
Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Menus, ComCtrls, DB, ADODB;
const
START = 9;
CMD = 0;
ERR = 1;
FRONT = 2;
MEG = 3;
REP = 4;
FRTREP =5 ;
type
TThreadQuery = class(TThread) { 声明线程类 }
private
QueryType: String;
{ 线程中的查询组件 }
protected
procedure Execute;
override;{ 执行线程的方法 }
public
constructor Create(suspended: Boolean);
{ 线程构造器 }
destructor Destroy;override;
end;
TForm1 = class(TForm)
GroupBox1: TGroupBox;
MainMenu1: TMainMenu;
N1: TMenuItem;
N2: TMenuItem;
RichEdit1: TRichEdit;
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure N2Click(Sender: TObject);
procedure DisplayMsg(msgtype:integer;Data:Ansistring;flag:Ansistring);
private
{ Private declarations }
public
{ Public declarations }
ThreadQuery: TThreadQuery;
{ 线程查询对象1 }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.DisplayMsg(msgtype:integer;Data:Ansistring;flag:Ansistring);
var
FileName:String;
f:system.textfile;
begin
FileName:=formatdatetime('yyyymmdd',now)+'.txt';
if not DirectoryExists(ExtractFilePath(Application.ExeName)+'Log') then
SysUtils.CreateDir(ExtractFilePath(Application.ExeName)+'Log');
AssignFile(f,ExtractFilePath(Application.ExeName)+'Log/'+ FileName);
if not FileExists(ExtractFilePath(Application.ExeName)+'Log/'+ FileName) then
Rewrite(f);
if FileExists(ExtractFilePath(Application.ExeName)+'Log/'+ FileName) then
Append(f);
with RichEdit1do
begin
Try
begin
if Lines.Count>100 then
Lines.Clear;
case msgtype of
CMD:begin
SelAttributes.Color := clGreen;
Lines.Add(FormatDateTime('hh:nn:ss',now)+'|开始|'+Data);
Repaint;
if flag = '1' then
Writeln(f,FormatDateTime('yyyymmdd|hh:nn:ss',now)+'|开始|'+Data);
end;
ERR:begin
SelAttributes.Color := clRed;
Lines.Add(FormatDateTime('hh:nn:ss',now)+'|失败|'+Data);
Repaint;
if flag = '1' then
Writeln(f,FormatDateTime('yyyymmdd|hh:nn:ss',now)+'|失败|'+Data);
end;
FRONT:begin
SelAttributes.Color := clBlue;
Lines.Add(FormatDateTime('hh:nn:ss',now)+'|执行|'+Data);
Repaint;
if flag = '1' then
Writeln(f,FormatDateTime('yyyymmdd|hh:nn:ss',now)+'|执行|'+Data);
end;
MEG:begin
SelAttributes.Color := clBlue;
Lines.Add(FormatDateTime('hh:nn:ss',now)+'|信息|'+Data);
if flag = '1' then
Writeln(f,FormatDateTime('yyyymmdd|hh:nn:ss',now)+'|信息|'+Data);
Repaint;
end;
REP:begin
SelAttributes.Color := clBlack;
Lines.Add(FormatDateTime('hh:nn:ss',now)+'|完成|'+Data);
Repaint;
if flag = '1' then
Writeln(f,FormatDateTime('yyyymmdd|hh:nn:ss',now)+'|完成|'+Data);
end;
FRTREP:begin
SelAttributes.Color := clBlue;
Lines.Add(FormatDateTime('hh:nn:ss',now)+'|退出|'+Data);
Repaint;
if flag = '1' then
Writeln(f,FormatDateTime('yyyymmdd|hh:nn:ss',now)+'|RECVE|'+Data);
end;
START:begin
SelAttributes.Color := clTeal;
Lines.Add(FormatDateTime('hh:nn:ss',now)+'|初始化|'+data);
Repaint;
if flag = '1' then
Writeln(f,FormatDateTime('yyyymmdd|hh:nn:ss',now)+'|初始化|'+Data);
end;
end;
{case}
end;
Except
SelAttributes.Color := clRed;
Lines.Add(FormatDateTime('hh:nn:ss',now)+'|ERR|[非法字符串或超长]');
end;
{Try}
end;
{with}
CloseFile(f);
end;
{ TThreadQuery类的实现 }
destructor TThreadQuery.Destroy;
begin
inherited destroy;
end;
procedure TThreadQuery.Execute;{ 执行线程的方法 }
var
zoneno,spotno,upspotno:String;
Dqquery,Dqquery1:TAdoquery;
begin
try
Dqquery:=TAdoquery.Create(nil);
Dqquery.Connection:=Form1.ADOConnection1;
with Dqquerydo
begin
if not active then
begin
Dqquery.close;
Dqquery.sql.clear;
Dqquery.sql.add('select zoneno ,spotno,upspotno from updatespot');
try
Dqquery.open;
except
Form1.displaymsg(Err,'数据库出错','1');
Form1.displaymsg(FRTREP,'操作','1');
exit;
end;
end;
first;
while not eofdo
begin
zoneno:=Dqquery.FieldByName('zoneno').asString;
spotno:=Dqquery.FieldByName('spotno').asString;
upspotno:=Dqquery.FieldByName('upspotno').asString;
if Dqquery.FieldByName('upspotno').IsNull then
begin
Form1.displaymsg(REP,'检查','1');
end
else
begin
Dqquery1:=TAdoquery.Create(nil);
Dqquery1.Connection:=Form1.ADOConnection1;
with Dqquery1do
begin
close;
sql.clear;
sql.Add('update pregcard');
sql.add(' set yxspot= '''+upspotno+'''');
sql.add(' where yxspot= '''+spotno+'''');
sql.add(' and zoneno= '''+zoneno+'''');
try
try
Form1.DisplayMsg(CMD,sql.Text,'1');
execsql;
except
Form1.displaymsg(Err,'更新失败','1');
Form1.displaymsg(FRTREP,'操作','1');Close;
exit;
end;
Form1.displaymsg(REP,'更新个人支行号','1');
finally
Dqquery1.Close;
end;
{try}
end;
end;
next;
end;
Dqquery.Close;
end;
Dqquery.Connection.Close;
except
ShowMessage('线程异常!');
{ 线程异常 }
end;
end;
{ 线程查询类的构造器 }
constructor TThreadQuery.Create(suspended: Boolean);
begin
inherited Create(suspended);
end;
procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
ThreadQuery.Terminate;
{ 销毁之前终止线程执行 }
ThreadQuery.Destroy;
end;
procedure TForm1.N2Click(Sender: TObject);
begin
try
displaymsg(start,'创建线程...','1');
ThreadQuery:= TThreadQuery.Create(True);
ThreadQuery.Resume;
displaymsg(start,'创建线程成功!','1');
except
displaymsg(Err,'创建线程失败!','1');
end;
//try
displaymsg(start,'创建线程完成!','1');
end;
end.
==============================
执行报:"尚未调用CoInitialize"错误
不知道是什么地方有问题,请大虾帮忙!
小弟刚刚在学线程
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Menus, ComCtrls, DB, ADODB;
const
START = 9;
CMD = 0;
ERR = 1;
FRONT = 2;
MEG = 3;
REP = 4;
FRTREP =5 ;
type
TThreadQuery = class(TThread) { 声明线程类 }
private
QueryType: String;
{ 线程中的查询组件 }
protected
procedure Execute;
override;{ 执行线程的方法 }
public
constructor Create(suspended: Boolean);
{ 线程构造器 }
destructor Destroy;override;
end;
TForm1 = class(TForm)
GroupBox1: TGroupBox;
MainMenu1: TMainMenu;
N1: TMenuItem;
N2: TMenuItem;
RichEdit1: TRichEdit;
ADOConnection1: TADOConnection;
ADOQuery1: TADOQuery;
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
procedure N2Click(Sender: TObject);
procedure DisplayMsg(msgtype:integer;Data:Ansistring;flag:Ansistring);
private
{ Private declarations }
public
{ Public declarations }
ThreadQuery: TThreadQuery;
{ 线程查询对象1 }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.DisplayMsg(msgtype:integer;Data:Ansistring;flag:Ansistring);
var
FileName:String;
f:system.textfile;
begin
FileName:=formatdatetime('yyyymmdd',now)+'.txt';
if not DirectoryExists(ExtractFilePath(Application.ExeName)+'Log') then
SysUtils.CreateDir(ExtractFilePath(Application.ExeName)+'Log');
AssignFile(f,ExtractFilePath(Application.ExeName)+'Log/'+ FileName);
if not FileExists(ExtractFilePath(Application.ExeName)+'Log/'+ FileName) then
Rewrite(f);
if FileExists(ExtractFilePath(Application.ExeName)+'Log/'+ FileName) then
Append(f);
with RichEdit1do
begin
Try
begin
if Lines.Count>100 then
Lines.Clear;
case msgtype of
CMD:begin
SelAttributes.Color := clGreen;
Lines.Add(FormatDateTime('hh:nn:ss',now)+'|开始|'+Data);
Repaint;
if flag = '1' then
Writeln(f,FormatDateTime('yyyymmdd|hh:nn:ss',now)+'|开始|'+Data);
end;
ERR:begin
SelAttributes.Color := clRed;
Lines.Add(FormatDateTime('hh:nn:ss',now)+'|失败|'+Data);
Repaint;
if flag = '1' then
Writeln(f,FormatDateTime('yyyymmdd|hh:nn:ss',now)+'|失败|'+Data);
end;
FRONT:begin
SelAttributes.Color := clBlue;
Lines.Add(FormatDateTime('hh:nn:ss',now)+'|执行|'+Data);
Repaint;
if flag = '1' then
Writeln(f,FormatDateTime('yyyymmdd|hh:nn:ss',now)+'|执行|'+Data);
end;
MEG:begin
SelAttributes.Color := clBlue;
Lines.Add(FormatDateTime('hh:nn:ss',now)+'|信息|'+Data);
if flag = '1' then
Writeln(f,FormatDateTime('yyyymmdd|hh:nn:ss',now)+'|信息|'+Data);
Repaint;
end;
REP:begin
SelAttributes.Color := clBlack;
Lines.Add(FormatDateTime('hh:nn:ss',now)+'|完成|'+Data);
Repaint;
if flag = '1' then
Writeln(f,FormatDateTime('yyyymmdd|hh:nn:ss',now)+'|完成|'+Data);
end;
FRTREP:begin
SelAttributes.Color := clBlue;
Lines.Add(FormatDateTime('hh:nn:ss',now)+'|退出|'+Data);
Repaint;
if flag = '1' then
Writeln(f,FormatDateTime('yyyymmdd|hh:nn:ss',now)+'|RECVE|'+Data);
end;
START:begin
SelAttributes.Color := clTeal;
Lines.Add(FormatDateTime('hh:nn:ss',now)+'|初始化|'+data);
Repaint;
if flag = '1' then
Writeln(f,FormatDateTime('yyyymmdd|hh:nn:ss',now)+'|初始化|'+Data);
end;
end;
{case}
end;
Except
SelAttributes.Color := clRed;
Lines.Add(FormatDateTime('hh:nn:ss',now)+'|ERR|[非法字符串或超长]');
end;
{Try}
end;
{with}
CloseFile(f);
end;
{ TThreadQuery类的实现 }
destructor TThreadQuery.Destroy;
begin
inherited destroy;
end;
procedure TThreadQuery.Execute;{ 执行线程的方法 }
var
zoneno,spotno,upspotno:String;
Dqquery,Dqquery1:TAdoquery;
begin
try
Dqquery:=TAdoquery.Create(nil);
Dqquery.Connection:=Form1.ADOConnection1;
with Dqquerydo
begin
if not active then
begin
Dqquery.close;
Dqquery.sql.clear;
Dqquery.sql.add('select zoneno ,spotno,upspotno from updatespot');
try
Dqquery.open;
except
Form1.displaymsg(Err,'数据库出错','1');
Form1.displaymsg(FRTREP,'操作','1');
exit;
end;
end;
first;
while not eofdo
begin
zoneno:=Dqquery.FieldByName('zoneno').asString;
spotno:=Dqquery.FieldByName('spotno').asString;
upspotno:=Dqquery.FieldByName('upspotno').asString;
if Dqquery.FieldByName('upspotno').IsNull then
begin
Form1.displaymsg(REP,'检查','1');
end
else
begin
Dqquery1:=TAdoquery.Create(nil);
Dqquery1.Connection:=Form1.ADOConnection1;
with Dqquery1do
begin
close;
sql.clear;
sql.Add('update pregcard');
sql.add(' set yxspot= '''+upspotno+'''');
sql.add(' where yxspot= '''+spotno+'''');
sql.add(' and zoneno= '''+zoneno+'''');
try
try
Form1.DisplayMsg(CMD,sql.Text,'1');
execsql;
except
Form1.displaymsg(Err,'更新失败','1');
Form1.displaymsg(FRTREP,'操作','1');Close;
exit;
end;
Form1.displaymsg(REP,'更新个人支行号','1');
finally
Dqquery1.Close;
end;
{try}
end;
end;
next;
end;
Dqquery.Close;
end;
Dqquery.Connection.Close;
except
ShowMessage('线程异常!');
{ 线程异常 }
end;
end;
{ 线程查询类的构造器 }
constructor TThreadQuery.Create(suspended: Boolean);
begin
inherited Create(suspended);
end;
procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
ThreadQuery.Terminate;
{ 销毁之前终止线程执行 }
ThreadQuery.Destroy;
end;
procedure TForm1.N2Click(Sender: TObject);
begin
try
displaymsg(start,'创建线程...','1');
ThreadQuery:= TThreadQuery.Create(True);
ThreadQuery.Resume;
displaymsg(start,'创建线程成功!','1');
except
displaymsg(Err,'创建线程失败!','1');
end;
//try
displaymsg(start,'创建线程完成!','1');
end;
end.
==============================
执行报:"尚未调用CoInitialize"错误
不知道是什么地方有问题,请大虾帮忙!
小弟刚刚在学线程