查询数据库,长时间等待时的动态提示,我自己做了一个,但有个问题:当执行一段存储过程时,若抛出错误(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,还有其他方法吗?

解决方案 »

  1.   

    我觉得线程中的异常和Button2Click中的异常不是一个异常,
    前一个异常好像被改造了
    可能是这个原因吧。
    顺便问一下,我用一个ADOQUERY组件执行一个时间较长的查询时,
    想用一个进度条现实查询的进度,要真的进度条,
    显示已查询的记录数怎么实现?
      

  2.   

    使用adoquery做进度,要将一个异步查询的选项设置好,不是太好做
      

  3.   

    是啊,比如正确的错误消息是:SQL Server Error: SQL State: 42000, SQL Error Code: 5000。
    可是捕获的消息却是:DBX Error .........
      

  4.   

    终于找到了答案
    http://dn.codegear.com/cn/print/37092