Re: Delphi type events in C#

Tech-Archive recommends: Speed Up your PC by fixing your registry



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).
.



Relevant Pages

  • Re: Cross thread question .....
    ... I have an app that reads data params from a stream and updates ... the main thread that created the controls. ... Your stream thread just reads results and updates the "neutral zone" ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Cross thread question .....
    ... What if you create a delegate and pass this delegate as a parameter to your ... I have an app that reads data params from a stream and updates ... The stream reader is on a different thread than ... the main thread that created the controls. ...
    (microsoft.public.dotnet.languages.csharp)
  • Cross thread question .....
    ... I have an app that reads data params from a stream and updates ... The stream reader is on a different thread than ... the main thread that created the controls. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Cross thread question .....
    ... I have an app that reads data params from a stream and updates ... The stream reader is on a different thread than ... the main thread that created the controls. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Delphi type events in C#
    ... handlers are executed synchronously (though really it's up to the ... this only works for single-cast delegates. ... that one other feature of auto-generated event accessors is that they ... it automatically closes the underlying stream. ...
    (microsoft.public.dotnet.languages.csharp)