H
happycyp
Unregistered / Unconfirmed
GUEST, unregistred user!
现在做了一个定时程序,因为一个操作比较耗时,会影响主程序反应迟钝,所以把它放到线程中运行,没想到,主程序还是受到影响,一点没有改善. 各位帮我看一下,如何改进?
代码如下:
unit UCMsgThread;
interface
uses
ExtCtrls,Classes,Forms;
type TMsgThread = class(TThread)
private
TMMsg :TTimer;
procedure TimerOnTimer(Sender: TObject);
procedure CreateTimer;
procedure DestroyTimer;
procedure RunTimer;
published
procedure Execute ;
override;
public
constructor Create(CreateSuspended: Boolean);
destructor Destroy;
override;
end;
implementation
uses USMS, UCHint;
{ TMsgThread }
constructor TMsgThread.Create(CreateSuspended: Boolean);
begin
inherited Create(CreateSuspended);
FreeOnTerminate := true;
Synchronize(CreateTimer);
end;
procedure TMsgThread.CreateTimer;
begin
TMMsg := TTimer.Create(nil);
end;
destructor TMsgThread.Destroy;
begin
Synchronize(DestroyTimer);
inherited destroy;
end;
procedure TMsgThread.DestroyTimer;
begin
TMMsg.Free;
end;
procedure TMsgThread.Execute;
begin
inherited;
Application.ProcessMessages;
RunTimer;
end;
procedure TMsgThread.RunTimer;
begin
TMMsg.Interval := 200;
TMMsg.OnTimer := TimerOnTimer;
TMMsg.Enabled := True;
end;
procedure TMsgThread.TimerOnTimer(Sender: TObject);
begin
{我的代码,耗时操作,
对串口操作}
end;
代码如下:
unit UCMsgThread;
interface
uses
ExtCtrls,Classes,Forms;
type TMsgThread = class(TThread)
private
TMMsg :TTimer;
procedure TimerOnTimer(Sender: TObject);
procedure CreateTimer;
procedure DestroyTimer;
procedure RunTimer;
published
procedure Execute ;
override;
public
constructor Create(CreateSuspended: Boolean);
destructor Destroy;
override;
end;
implementation
uses USMS, UCHint;
{ TMsgThread }
constructor TMsgThread.Create(CreateSuspended: Boolean);
begin
inherited Create(CreateSuspended);
FreeOnTerminate := true;
Synchronize(CreateTimer);
end;
procedure TMsgThread.CreateTimer;
begin
TMMsg := TTimer.Create(nil);
end;
destructor TMsgThread.Destroy;
begin
Synchronize(DestroyTimer);
inherited destroy;
end;
procedure TMsgThread.DestroyTimer;
begin
TMMsg.Free;
end;
procedure TMsgThread.Execute;
begin
inherited;
Application.ProcessMessages;
RunTimer;
end;
procedure TMsgThread.RunTimer;
begin
TMMsg.Interval := 200;
TMMsg.OnTimer := TimerOnTimer;
TMMsg.Enabled := True;
end;
procedure TMsgThread.TimerOnTimer(Sender: TObject);
begin
{我的代码,耗时操作,
对串口操作}
end;