X
xlh1981
Unregistered / Unconfirmed
GUEST, unregistred user!
程序代码如下:
unit Unit3;
interface
uses
Classes;
type
TAnalyzeThread = class(TThread)
private
{ Private declarations }
procedure Update;
protected
procedure Execute;
override;
end;
implementation
uses Unit1;
{ 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 TAnalyzeThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ TAnalyzeThread }
procedure TAnalyzeThread.Update;
begin
Form1.Memo1.Lines.Append('Analyze and Display Thread::Now Analyze and Display');{}
end;
procedure TAnalyzeThread.Execute;
begin
{ Place thread code here }
while(Form1.ReadFileOKEvent.WaitFor(100)=wrSignaled)do//错误在此
begin
if(Terminated)then
exit;
Form1.AnalyzeOKEvent.ResetEvent;
Synchronize(Update);
Form1.AnalyzeOKEvent.SetEvent();
end;
end;
end.
主窗口代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, StdCtrls,syncobjs,Unit2,Unit3;
type
TForm1 = class(TForm)
MainMenu1: TMainMenu;
Memo1: TMemo;
File1: TMenuItem;
Start11: TMenuItem;
Exit11: TMenuItem;
procedure Start11Click(Sender: TObject);
procedure Exit11Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
ReadFileOKEvent:TEvent;//读文件事件对象
AnalyzeOKEvent:TEvent;//数据分析事件对象
ReadFileThread:TReadFileThread;//文件读取线程对象
AnalyzeThread:TAnalyzeThread;//数据分析线程对象
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Start11Click(Sender: TObject);
begin
ReadFileThread.Resume;
AnalyzeThread.Resume;
end;
procedure TForm1.Exit11Click(Sender: TObject);
begin
Close;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ReadFileOKEvent:=TEvent.Create(nil,true,false,'ReadThread');
ReadFileOKEvent.ResetEvent;
AnalyzeOKEvent:=TEvent.Create(nil,true,true,'AnalyzeThread');
AnalyzeOkEvent.SetEvent;
ReadFileThread:=TReadFileThread.Create(true);
ReadFileThread.FreeOnTerminate:=false;
AnalyzeThread:=TAnalyzeThread.Create(true);
AnalyzeThread.FreeOnTerminate:=false;
end;
procedure TForm1.FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
begin
ReadFileThread.Terminate;
AnalyzeThread.Terminate;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
ReadFileOKEvent.Free;
AnalyzeOKEvent.Free;
ReadFileThread.Free;
AnalyzeThread.Free;
Memo1.Clear;
end;
end.
错误是不识别wrSignaled.
unit Unit3;
interface
uses
Classes;
type
TAnalyzeThread = class(TThread)
private
{ Private declarations }
procedure Update;
protected
procedure Execute;
override;
end;
implementation
uses Unit1;
{ 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 TAnalyzeThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ TAnalyzeThread }
procedure TAnalyzeThread.Update;
begin
Form1.Memo1.Lines.Append('Analyze and Display Thread::Now Analyze and Display');{}
end;
procedure TAnalyzeThread.Execute;
begin
{ Place thread code here }
while(Form1.ReadFileOKEvent.WaitFor(100)=wrSignaled)do//错误在此
begin
if(Terminated)then
exit;
Form1.AnalyzeOKEvent.ResetEvent;
Synchronize(Update);
Form1.AnalyzeOKEvent.SetEvent();
end;
end;
end.
主窗口代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, StdCtrls,syncobjs,Unit2,Unit3;
type
TForm1 = class(TForm)
MainMenu1: TMainMenu;
Memo1: TMemo;
File1: TMenuItem;
Start11: TMenuItem;
Exit11: TMenuItem;
procedure Start11Click(Sender: TObject);
procedure Exit11Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
ReadFileOKEvent:TEvent;//读文件事件对象
AnalyzeOKEvent:TEvent;//数据分析事件对象
ReadFileThread:TReadFileThread;//文件读取线程对象
AnalyzeThread:TAnalyzeThread;//数据分析线程对象
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Start11Click(Sender: TObject);
begin
ReadFileThread.Resume;
AnalyzeThread.Resume;
end;
procedure TForm1.Exit11Click(Sender: TObject);
begin
Close;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ReadFileOKEvent:=TEvent.Create(nil,true,false,'ReadThread');
ReadFileOKEvent.ResetEvent;
AnalyzeOKEvent:=TEvent.Create(nil,true,true,'AnalyzeThread');
AnalyzeOkEvent.SetEvent;
ReadFileThread:=TReadFileThread.Create(true);
ReadFileThread.FreeOnTerminate:=false;
AnalyzeThread:=TAnalyzeThread.Create(true);
AnalyzeThread.FreeOnTerminate:=false;
end;
procedure TForm1.FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
begin
ReadFileThread.Terminate;
AnalyzeThread.Terminate;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
ReadFileOKEvent.Free;
AnalyzeOKEvent.Free;
ReadFileThread.Free;
AnalyzeThread.Free;
Memo1.Clear;
end;
end.
错误是不识别wrSignaled.