TThreadMethod and TThreadProcedure

來源:互聯網
上載者:User

標籤:style   http   color   io   os   ar   for   sp   c   

http://embarcadero.newsgroups.archived.at/public.delphi.rtl/201112/1112035763.html

> Hi,
>
> What is the difference between these two definitions:
>
>  TThreadMethod = procedure of object;
>  TThreadProcedure = reference to procedure;
>
> Please can you try to explain simply :)

Well, it is not exactly a simple subject ;-)

TThreadMethod declares a method pointer type.

Internally, a method pointer is represented as a record of two pointers (there is a type TMethod for that).

TMethod = record
    code: pointer;
    data: pointer
  end;

If you declare a variable of a method pointer type you can only assign
it a method of an object. Wayne‘s example is wrong here since it uses
the @ operator. You do not use that for assigning a method to a method
pointer variable.

    TMyClass = class
    private
        MyMethod: ThreadMethod;
        procedure Foo;

  MyMethod := Foo;

On this assignment the compiler internally executes

  TMethod(MyMethod).Code := @TMyClass.Foo;
  TMethod(MyMethod).Data := self;  // actually the address of the object Foo belongs to

Calling the method through the method pointer will pass the Data value
as the hidden Self parameter to Foo. So you can only use a method
pointer safely as long as the object the pointer refers to still exists.

TThreadProcedure = reference to procedure;

This declares a new-fangled thing called an anonymous method, but it is
in fact more generally useful than the use  Wayne showed. To a variable
of type TThreadProcedure you could assign not only a nameless procedure
constructed on the fly, but also an existing method of some object, or
even a standalone procedure. The compiler does a lot more work behind
the scene for these types. It creates a whole class with a matching
interface type, creates an instance, stores any local variables the
anonymous method refers to into this instance, implements the anonymous
method as a method of this class, calls it, cleans up afterwards.
"reference to x" types are not simple types, unlike method or function
pointer types. They have considerable overhead in terms of code size
and execution, but are a lot more flexible in turn.

Anonymous methods are Delphi‘s implementation of a computer science
concept called a closure (see
http://en.wikipedia.org/wiki/Closure_%28computer_science%29 )

-- 

TThreadMethod and TThreadProcedure

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.