您好,如果等待一个线程执行完后再作别的操作 ? 谢谢(50分)

  • 主题发起人 主题发起人 pcgod
  • 开始时间 开始时间
P

pcgod

Unregistered / Unconfirmed
GUEST, unregistred user!
您好, 创建一个线程后,要等待这个线程结束了在执行别的操作,该怎么做 ?
procedure TCSMListForm.FlatButton7Click(Sender: TObject);
begin
with T.Create(False) do
if Suspended then Resume ;
// 这儿应该怎么写 ? 谢谢您

end ;









 
t.onterminate=youfunc; //你的函数即可
 
t.onterminate:=youfunc;
 
youfunc必须是一个procedure (sender:TObject) of object;方法才行
 
线程是用来同时执行任务的,象你这样,还用得着线程吗?
 
TO zhukewen:、
这样做是有道理的,如果一个操作是组塞的,单独用线程做,消息循环可以正常进行
否则你在进行计算的时候,程序可能失去响应
 
张无忌 :

您好,请问
youfunc必须是一个procedure (sender:TObject) of object;方法才行
是什么含义 ? 一个对象的过程么 ? 谢谢
 
procedure TForm1.Button1Click(Sender: TObject);
var
StartupInfo: TStartupinfo;
ProcessInfo: TProcessInformation;
begin
FillChar(Startupinfo,Sizeof(TStartupinfo),0);
Startupinfo.cb:=Sizeof(TStartupInfo);
if CreateProcess(nil,
'notepad.exe',
nil,
nil,
false,
normal_priority_class,
nil,
'c:/winnt',
Startupinfo,
ProcessInfo) then
begin
WaitforSingleObject(Processinfo.hProcess, infinite);
CloseHandle(ProcessInfo.hProcess);
application.messagebox('program closed','program closed',0);
end;
end;
 
将 OnTerminate 指向一个类过程

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

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

var
Form1: TForm1;

implementation

{$R *.dfm}

uses Unit2 ;

procedure TForm1.a(Sender : TObject) ;
begin
ShowMessage('1234') ;
end ;

procedure TForm1.Button1Click(Sender: TObject);
var
t : TThTest ;
begin
t := TThTest.Create(False) ;
with t do
if Suspended then Resume;
t.OnTerminate := a ;

end;

end.

--------------------------------------------
TheThread :



unit Unit2;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TThTest = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
procedure Show ;
public
Constructor Create(B : Boolean) ;
end;

implementation

uses unit1 ;

{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example,

Synchronize(UpdateCaption);

and UpdateCaption could look like,

procedure TThTest.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }

{ TThTest }

Constructor TThTest.create(B : Boolean) ;
begin
Inherited Create(False) ;
end ;

procedure TThTest.Execute;
begin
{ Place thread code here }
Synchronize(Show)
end;

procedure TThTest.Show ;
var
i : Integer ;
DC : HDC ;
s : String ;
begin
DC := GetDC(Form1.Handle) ;
for i := 0 to 100000 do
begin
S:=Inttostr(i);
Textout(DC,10,10,Pchar(S),length(S));
end;
ReleaseDC(Form1.Handle,DC);
end ;

end.


 
多人接受答案了。
 
后退
顶部