How to Fix Properly "Symbol Resume is deprecated" Warning in Delphi 2010

If you'll upgrade to Delphi 2010, you'll quickly notice that old code you were using to create threads, such as

constructor TMyThread.Create;
begin
  inherited Create({CreateSuspended}true);
 // must create event handle first!
  FEventHandle := CreateEvent(
          {security}      nil,
          {bManualReset}  true,
          {bInitialState} false,
          {name}          nil);
  Resume;
end;

now produces a warning:

[DCC Warning] xxx.pas(277): W1000 Symbol ‘Resume’ is deprecated

Of course, you can change Resume to Start and it will work, but that's not a proper way to create threads. The proper way would be:

constructor TMyThread.Create;
begin
 // create event handle first!
  FEventHandle := CreateEvent(
          {security}      nil,
          {bManualReset}  true,
          {bInitialState} false,
          {name}          nil);
  inherited Create({CreateSuspended}false);
end;

The trick is that Delphi does not require "inherited Create" to be the first statement in descendant constructor body.

Calling TObject.Create from descendant constructor does not create an object and does not write zeros to all of object fields. It is TObject.NewInstance method that does that.

The call "fMyThread := TMyThread.Create;" is actually compiled into two calls:

  1. The 1st compiled call is to TObject.NewInstance() method; this method allocates memory and initializes the fields
  2. The 2nd compiled call is to TMyThread.Create, invoked as any other regular method.

The same principle also applies to Destroy and FreeInstance methods: calling ancestor Destroy is not required to be the last statement of destructor's body.

Comments

install Delphi SPComm (Delphi 7.0)

Hi,
I want to install a Serial-port tool into Delphi. I selcet the SPComm.
But when I install it within the methods it can not work.
And it prints the warning like:
->[Warning] AdTapi.pas(1832): Symbol 'AllocateHWnd' is deprecated
->[Warning] AdTapi.pas(1856): Symbol 'DeallocateHWnd' is deprecated
How to deal with it?
Looking forward to your reply.

E-mail:888hunt888@gmail.com or 853327011@qq.com

Post new comment

The content of this field is kept private and will not be shown publicly.