查询数据库,长时间等待时的动态提示,我自己做了一个,但有个问题:当执行一段存储过程时,若抛出错误(raiserror),那么在程序中得到的message并不正确。程序代码如下:----------------------------------------过程代码---------------------------
CREATE PROCEDURE sp_testproc --MSSQL2000
AS
BEGIN
RAISERROR('其它用户正在进行数据处理,请稍后!',16,1);
END---------------------------------提示窗口的pas,没有任何代码-------------------
unit uMsgBox;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, ComCtrls, GIFImage, StdCtrls;type
TMsgBox = class(TForm)
Panel1: TPanel;
Image1: TImage;
Label1: TLabel;
private
{ Private declarations }
public
{ Public declarations }
end;var
MsgBox: TMsgBox;implementation{$R *.dfm}end.
---------------------------------------执行SQL的线程pas-----------------------------
unit uMessageBoxThread;interfaceuses
Classes, SysUtils, Windows;type
TMessageBoxThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
public
proc:Pointer;
ErrMsg:string;
end;implementation{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure UpdateForm.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ UpdateForm }procedure TMessageBoxThread.Execute;
var
p:procedure;
begin
{ Place thread code here }
if proc<>nil then begin
p:=proc;
try
p;
except
on e:Exception do ErrMsg:=e.Message;
end;
end;
Suspend;
end;end.
-----------------------------------------主程序------------------------------------
//用线程后台执行sql的过程
procedure ShowMessageBox(text:string;proc:Pointer);
var
MsgThd:TMessageBoxThread;
MsgBox:TMsgBox;
ErrMsg:string;
begin
Application.MainForm.Enabled:=False;
MsgBox:=TMsgBox.Create(Application);
MsgBox.Label1.Caption:=text;
MsgBox.Show;
MsgThd:=TMessageBoxThread.Create(True);
MsgThd.proc:=proc;
MsgThd.Resume;
while not MsgThd.Suspended do begin
Application.ProcessMessages;
Sleep(10);
end;
ErrMsg:=MsgThd.ErrMsg;
MsgThd.Terminate;
MsgThd.Destroy;
MsgBox.Close;
MsgBox.Destroy;
Application.MainForm.Enabled:=True;
Application.BringToFront;
if ErrMsg<>EmptyStr then
raise Exception.Create(ErrMsg); ////<<<-----------------线程内捕获的异常消息不正确
end;
procedure u;
begin
Form1.SQLStoredProc1.ExecProc;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessageBox('正在审核[msd0012132]...',@u);
end;procedure TForm1.Button2Click(Sender: TObject);
begin
try
u;
except
on e:Exception do showmessage(e.message); ////<<<-----------------显示正常
end;
end;为什么线程捕获的消息和主进程中捕获的不一样,如果要在查询数据时,显示一个动态的gif或avi,还有其他方法吗?
CREATE PROCEDURE sp_testproc --MSSQL2000
AS
BEGIN
RAISERROR('其它用户正在进行数据处理,请稍后!',16,1);
END---------------------------------提示窗口的pas,没有任何代码-------------------
unit uMsgBox;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, ComCtrls, GIFImage, StdCtrls;type
TMsgBox = class(TForm)
Panel1: TPanel;
Image1: TImage;
Label1: TLabel;
private
{ Private declarations }
public
{ Public declarations }
end;var
MsgBox: TMsgBox;implementation{$R *.dfm}end.
---------------------------------------执行SQL的线程pas-----------------------------
unit uMessageBoxThread;interfaceuses
Classes, SysUtils, Windows;type
TMessageBoxThread = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
public
proc:Pointer;
ErrMsg:string;
end;implementation{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure UpdateForm.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ UpdateForm }procedure TMessageBoxThread.Execute;
var
p:procedure;
begin
{ Place thread code here }
if proc<>nil then begin
p:=proc;
try
p;
except
on e:Exception do ErrMsg:=e.Message;
end;
end;
Suspend;
end;end.
-----------------------------------------主程序------------------------------------
//用线程后台执行sql的过程
procedure ShowMessageBox(text:string;proc:Pointer);
var
MsgThd:TMessageBoxThread;
MsgBox:TMsgBox;
ErrMsg:string;
begin
Application.MainForm.Enabled:=False;
MsgBox:=TMsgBox.Create(Application);
MsgBox.Label1.Caption:=text;
MsgBox.Show;
MsgThd:=TMessageBoxThread.Create(True);
MsgThd.proc:=proc;
MsgThd.Resume;
while not MsgThd.Suspended do begin
Application.ProcessMessages;
Sleep(10);
end;
ErrMsg:=MsgThd.ErrMsg;
MsgThd.Terminate;
MsgThd.Destroy;
MsgBox.Close;
MsgBox.Destroy;
Application.MainForm.Enabled:=True;
Application.BringToFront;
if ErrMsg<>EmptyStr then
raise Exception.Create(ErrMsg); ////<<<-----------------线程内捕获的异常消息不正确
end;
procedure u;
begin
Form1.SQLStoredProc1.ExecProc;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessageBox('正在审核[msd0012132]...',@u);
end;procedure TForm1.Button2Click(Sender: TObject);
begin
try
u;
except
on e:Exception do showmessage(e.message); ////<<<-----------------显示正常
end;
end;为什么线程捕获的消息和主进程中捕获的不一样,如果要在查询数据时,显示一个动态的gif或avi,还有其他方法吗?
解决方案 »
- 【投诉黑庄】请管理员处理
- EAccessViolation 地址访问错误 高手请进
- ADO连接远程数据库的问题
- 送分!一个简单的数据查询问题。
- 不会吗,真的没有人能帮助我吗,谢谢各位大哥进来看看!
- 在delphi中我想解form中的事件触发顺序,不知在帮助中哪里找?
- 简单update 语句求教
- 有没有哪位高手可以帮我写一个判断日期合法性的函数?
- 如何在Report Machine报表中插入个chart
- 用Delphi的XmlBroker,MidasPagePoducer,DataSetProvider的朋友请进?急死我了
- 用adoquery查询,如何得到查询记录数?
- paradox读取备注字段错误的问题
前一个异常好像被改造了
可能是这个原因吧。
顺便问一下,我用一个ADOQUERY组件执行一个时间较长的查询时,
想用一个进度条现实查询的进度,要真的进度条,
显示已查询的记录数怎么实现?
可是捕获的消息却是:DBX Error .........
http://dn.codegear.com/cn/print/37092