请教如何可以在后台执行?(100分)

  • 主题发起人 主题发起人 holyszq
  • 开始时间 开始时间
H

holyszq

Unregistered / Unconfirmed
GUEST, unregistred user!
当客户按查询数据按钮时其它按钮就不能动了,如何实现
客户按下按钮后等等查询时还可用其它的按钮或输入数据等。
让用户感觉不到在查询数据库 。
谢谢!!!要有具体例子和原理。。。。。。
 
Application.ProcessMessage();
这个函数让应用程序处理消息,也就是可以响应那些 输入数据的 操作
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,shellapi;
const
// 定义 TARYICON的 id 号 用来区别多个ICON
trayiconid = 1;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
private
{ Private declarations }
// TRAYICON 图标
ficon : ticon;
// 定义TBOTIFYICONDATA的类型成员变量NID
nid : tnotifyicondata;
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
// 调入TRAYICON 如果出错 则推出
ficon := ticon.Create;
ficon.Handle:= loadicon(hinstance,'mainicon');
if ficon.Handle= 0 then
begin
messagebox(form1.Handle,'无法打开图标!','消息',mb_ok+mb_iconinformation);
application.Terminate;
exit;
end;

//填写NID数据结构
with niddo
begin
//CBSIZE设为本项记录的大小
cbsize := sizeof(tnotifyicondata);
//WMD窗口句柄
wnd := handle;
//UID设为TRAYICON的ID 号
uid := trayiconid;
uflags := nif_message or nif_icon or nif_tip;
ucallbackmessage :=0;
//HICON 设置为将要添加的图标句柄
hicon := ficon.Handle;
//SZTIP设置为相关提示信息
strpcopy(sztip,'这是一个测试演示程序!') ;
end;
// 放置TRAYICON图标到任务栏状态区
shell_notifyicon(nim_add,@nid);
//使应用程序在任务栏上隐藏
//参数WS_EX_TOOLWINDOW表示窗口是TOOLWINDOW,TOOLWINDOW不会显示在任务栏上
setwindowlong(application.Handle,gwl_exstyle,WS_EX_TOOLWINDOW);
end;

procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
//从任务栏上删除TRAYICON
shell_notifyicon(nim_delete,@nid);
//释放FICON
FICON.Free;
end;

end.
 
delphi目录下的Demos/Db/BkQuery
 
孤灯夜影 的答案好象牛头不对马嘴, 说的是另外的一个问题吧
 
贴一篇多线程后台查询的帖子:
unit Mainfrm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, DBTables, Buttons, Grids, DBGrids;
type
TfrmMain = class(TForm)
Database1: TDatabase;
Query1: TQuery;
DataSource1: TDataSource;
Database2: TDatabase;
Query2: TQuery;
DataSource2: TDataSource;
btnQuery: TSpeedButton;
DBGrid1: TDBGrid;
DBGrid2: TDBGrid;
Session2: TSession;
Session1: TSession;
procedure btnQueryClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

//后台查询数据库的线程类
TQueryThread = class(TThread)
private
FSession: TSession;
FDatabase: TDataBase;
FQuery: TQuery;
FDatasource: TDatasource;
FQueryException: Exception;
procedure ConnectDataSource;
procedure ShowQryError;
protected
procedure Execute;
override;
public
constructor Create(Session: TSession;
DataBase:
TDatabase;
Query: TQuery;
DataSource: TDataSource);virtual;
end;
var
frmMain: TfrmMain;
implementation
{$R *.dfm}
//TQueryThread的实现
constructor TQueryThread.Create(Session: TSession;
DataBase:
TDatabase;
Query: TQuery;
Datasource: TDataSource);
begin
inherited Create(False);
//以立即执行方式创建线程
//将传进来的数据库的相关对象赋给私有变量
FSession := Session;
FDatabase := DataBase;
FQuery := Query;
FDataSource := Datasource;
FreeOnTerminate := True;
// //当线程执行完,终止线程
end;
procedure TQueryThread.Execute;
begin
try
FQuery.Open;
Synchronize(ConnectDataSource);
except
FQueryException := ExceptObject as Exception;
Synchronize(ShowQryError);
end;
end;
procedure TQueryThread.ConnectDataSource;
begin
FDataSource.DataSet := FQuery;
//连接DataSource到数据集Query上
end;
procedure TQueryThread.ShowQryError;
begin
Application.ShowException(FQueryException);
end;
procedure RunBackgroundQuery(Session: TSession;
DataBase:
TDataBase;Query: TQuery;
DataSource:TDataSource);
begin
TQueryThread.Create(Session, Database, Query, DataSource);
end;
//TfrmMain实现
procedure TfrmMain.btnQueryClick(Sender: TObject);
begin
RunBackgroundQuery(Session1, DataBase1, Query1,Datasource1);
RunBackgroundQuery(Session2, DataBase2, Query2,Datasource2);
end;
end.
 
后退
顶部