Form运行时, 怎样才能最小化, 打开其他Form. (急!!)(100分)

  • 主题发起人 主题发起人 sercall
  • 开始时间 开始时间
S

sercall

Unregistered / Unconfirmed
GUEST, unregistred user!
有一个复杂查询Form1, 需几分钟运行.
怎样才能在查询Form1运行时,让它最小化,打开其他Form,做其他程序?
(我现在当运行Form1时,不能最小化,不能做其它Form)
谢谢!!
 
在进行复杂查询和计算(主要是在做报表汇总)时,系统资源几乎被100%占用,你想干别
的事都干不了。我的思路上创建一个线程(TThread)对象,并让它来执行查询过程。还
没实践过,试成功后再告诉你。
 
试试?(下面这个程序你可以试一下,如果不采用线程序来执行SumRec过程的话,在按下
Button1执行SumRec过程时Button2是无效的,而用线程执行的情况下Button2却是有效的)
虽然我设了FreeOnTerminate,但我现在还不能证实线程在执行完SumRec过程后会自动释放,
哪位大侠能给点意见?
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TMyThread = class(TThread)
protected
procedure Execute;
override;
public
constructor Create(CreateSuspended: Boolean);
end;

TForm1 = class(TForm)
Button1: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
procedure SumRec;
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
Thrd1: TMyThread;
implementation
{$R *.DFM}
constructor TMyThread.Create(CreateSuspended: Boolean);
begin
inherited;
Priority := tpLower;
//设线程的优先级为较低
FreeOnTerminate := True;
//在线程中止时释放线程
end;

procedure TMyThread.Execute;
begin
Form1.SumRec;
end;

procedure TForm1.SumRec;
var I: Integer;
begin
I:=0;
while I<1000000000do
Inc(I,1);
MessageBox(Handle,PChar(IntToStr(I)+''),'',MB_OK);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Thrd1 := TMyThread.Create(False);
//False表示一创建就开始执行线程
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
ShowMessage('Hello, World!');
end;

end.
 
做一点更改,可以放心使用了。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TMyThread = class(TThread)
protected
procedure Execute;
override;
public
constructor Create(CreateSuspended: Boolean);
end;

TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
procedure SumRec;
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
Thrd1: TMyThread;
implementation
{$R *.DFM}
constructor TMyThread.Create(CreateSuspended: Boolean);
begin
inherited;
Priority := tpLower;
//设线程的优先级为较低
end;

procedure TMyThread.Execute;
begin
FreeOnTerminate := True;
//在线程中止时释放线程
Form1.SumRec;
//把SumRec过程替换为你的查询过程就可以了
end;

procedure TForm1.SumRec;
var I: Integer;
begin
I:=0;
while I<1000000000do
Inc(I,1);
MessageBox(Handle,PChar(IntToStr(I)+''),'',MB_OK);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Thrd1 := TMyThread.Create(False);
//False表示一创建就开始执行线程
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
ShowMessage('Hello, World!');
end;

end.
 
接受答案了.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部