Re: Delphi type events in C#
- From: Pavel Minaev <int19h@xxxxxxxxx>
- Date: Mon, 9 Mar 2009 19:51:48 -0700 (PDT)
On Mar 9, 7:34 pm, "JM" <m...@xxxxxxxx> wrote:
Just to make sure that I have this down, is the following psuedo code
correct?
If I have control1, control2 and control3 and each subscribes to event1,
then when event1 fires, each of the controls would be notified?
Yes, though technically, the subscribers are not controls, but
specific methods.
Are the events asynchronous - basically fire and forget?
No, handlers are executed synchronously (though really it's up to the
component - it can, of course, enumerate the list of handlers, and
spawn an individual thread for each).
Are the controls notified in the order in which they initially subscribed to the event?
Yes.
I'm guessing the delegate/event has to contain some sort of stack, queue (or more
probably list) of subscribers and the logic to control them - yes?
Yes, a multicast delegate is essentially just a list, where each
element is the receiver - or null for static methods - and pointer to
actual method code.
Next up, streams with readers/writers attached! :-)
With Delphi, you create the stream and then attach it to the reader/writer
via the reader/writer's constructor. Then, when done, you have to destroy
the reader/writer and then the stream. If you destroy the stream first, then
the reader/writer ends up with a dangling pointer to the stream and kaboom -
your basic memory leak or much worse..
AStream := TMemorystream.Create;
Writer := TWriter.Create(AStream, 1024); // 1024 byte buffer
With Writer Do Try
WriteBoolean(true);
WriteInteger(-1);
// etc
Finally
Writer.Free; <- first
AStream.Free; <- second
End;
Does this hold true in C# as well?
No. When you do StreamWriter.Close() (or use the using-block, which is
what you normally do), it automatically closes the underlying stream.
If you close the stream first, then the writer will of course be
unusable, but there won't be any dangling references or memory leaks -
no such thing in C# (thanks to GC).
.
- Follow-Ups:
- Re: Delphi type events in C#
- From: JM
- Re: Delphi type events in C#
- From: Peter Morris
- Re: Delphi type events in C#
- References:
- Delphi type events in C#
- From: JM
- Re: Delphi type events in C#
- From: Pavel Minaev
- Re: Delphi type events in C#
- From: JM
- Re: Delphi type events in C#
- From: Pavel Minaev
- Re: Delphi type events in C#
- From: JM
- Delphi type events in C#
- Prev by Date: Re: Delphi type events in C#
- Next by Date: Re: download site html
- Previous by thread: Re: Delphi type events in C#
- Next by thread: Re: Delphi type events in C#
- Index(es):
Relevant Pages
|