| 发表于:2007-11-17 18:02:39 楼主 |
查询数据库,长时间等待时的动态提示,我自己做了一个,但有个问题:当执行一段存储过程时,若抛出错误(raiserror),那么在程序中得到的message并不正确。程序代码如下: ----------------------------------------过程代码--------------------------- create procedure sp_testproc --mssql2000 as begin raiserror('其它用户正在进行数据处理,请稍后!',16,1); end ---------------------------------提示窗口的pas,没有任何代码------------------- unit umsgbox; interface uses 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; interface uses 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,还有其他方法吗? |
|
|
|
|