softbreeze
asked on
Deprecated Delphi TThread.Suspend
I've noticed that the TThread.Suspend and Resume methods have been deprecated in Delphi XE2. I would like to see an sample of what would be an appropriate replacement for them. I have included a sample program that contains logic I use quite a bit. It is simply a running clock in a TStatusBar component that updates every second using Synchronize to allow the thread to update the form. Usually this is the only additional thread in my application, however that is not always the case. Here is the code and thank you for any appropriate Suspend/Resume code replacement:
unit DemoFrm;
interface
uses
Winapi.Windows, Winapi.Messages,
System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls,
Vcl.ExtCtrls;
const
WM_APPSTARTUP = WM_USER + 1;
type
TDemoForm = class(TForm)
StatusBar: TStatusBar;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormShow(Sender: TObject);
private
procedure WMAppStartup(var Msg: TMessage); message WM_APPSTARTUP;
procedure ClockTick(Sender: TObject);
procedure UpdateStatusBar(aIndex: Integer; aMsg: String);
public
{ Public declarations }
end;
TClockThread = class(TTHread)
private
procedure UpdateStatus;
public
procedure Execute; override;
end;
var
DemoForm: TDemoForm;
implementation
{$R *.dfm}
var
ClockTID: TClockThread;
ClockTimer: TTimer;
procedure TClockThread.UpdateStatus;
begin
if ((Assigned(DemoForm)) and (Assigned(ClockTimer))) then
begin
with (DemoForm) do
begin
UpdateStatusBar(0,FormatDateTime('h:nn:ss am/pm',Now));
end;
end;
end;
procedure TClockThread.Execute;
begin
while (TRUE) do
begin
Synchronize(UpdateStatus);
Suspend;
end;
end;
procedure TDemoForm.WMAppStartup(var Msg: TMessage);
begin
Update;
ClockTimer.Enabled := TRUE;
end;
procedure TDemoForm.ClockTick(Sender: TObject);
begin
if (Assigned(ClockTID)) then
ClockTID.Resume;
end;
procedure TDemoForm.UpdateStatusBar(aIndex: Integer; aMsg: String);
begin
if (aIndex < StatusBar.Panels.Count) then
begin
StatusBar.Panels[aIndex].Text := aMsg;
StatusBar.Update;
end;
end;
procedure TDemoForm.FormCreate(Sender: TObject);
begin
ClockTID := TClockThread.Create(FALSE);
ClockTimer := TTimer.Create(nil);
ClockTimer.Enabled := FALSE;
ClockTimer.Interval := 1000;
ClockTimer.OnTimer := ClockTick;
end;
procedure TDemoForm.FormDestroy(Sender: TObject);
begin
if (Assigned(ClockTimer)) then ClockTimer.Free;
end;
procedure TDemoForm.FormShow(Sender: TObject);
begin
PostMessage(Handle,WM_APPSTARTUP,0,0);
end;
end.
// DFM
object DemoForm: TDemoForm
Left = 0
Top = 0
Caption = 'Status Clock Demo'
ClientHeight = 297
ClientWidth = 383
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
OnDestroy = FormDestroy
OnShow = FormShow
PixelsPerInch = 96
TextHeight = 13
object StatusBar: TStatusBar
Left = 0
Top = 278
Width = 383
Height = 19
Panels = <
item
Alignment = taCenter
Width = 90
end
item
Width = 50
end>
end
end
According to the Help for Delphi XE they suggest to use the TEvent component instead of the Suspend and Resume methods.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you, karagunes! You'll get the points. I will release them to you tomorrow after I take a closer look at your example.
Over the weekend I found some articles and created some sample code that use the TEvent.SetEvent/ResetEvent as well as PostMessage calls. From them I was able to accomplish what I was trying to do.
I'd like to post the code to the site, just in case in the future I go looking for this again.
Thanks again
Over the weekend I found some articles and created some sample code that use the TEvent.SetEvent/ResetEvent
I'd like to post the code to the site, just in case in the future I go looking for this again.
Thanks again
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, Both of you answered my questions, Geert's answer actually was closer to what I was asking.