Solved

Urgent: Event Handler Recursion

Posted on 2004-09-16
7
679 Views
Last Modified: 2012-06-27
Does anyone know what the exact behavior of the following scenario would be:

1. Class c1 has an event e1.
2. Class c2 has an event handler eh1
3. Instance c2a's eh1 method is attached as a handler for the event c1a.e1.
4. Event c1a.e1 is raised once.
5. Inside the c2a.eh1 handler, 2 new instances of c2 (c2b and c2c) are instantiated, and their eh1 methods are attached as handlers for the same event c1a.e1.

So the question is, will the method eh1 get called 1 time, or 3 times, or is it ambiguous?

Or in other words, at what point during the raising of an event does adding/removing handlers stop applying to the event already being raised?

Thanks!
0
Comment
Question by:Justin_W
  • 4
  • 3
7 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 12079892
using System;

namespace ConsoleApplication11
{

      public delegate void FooDelegate();
      public class foo
      {
            public event FooDelegate Fooed;
            private int i=0;
            public string name;

            public void OnFoo()
            {
                  i++;
                  if (Fooed != null)
                        Fooed();
            }

            public void eh1()
            {
                  Console.WriteLine(name + ":eh1 called");
                  foo f1 = new foo();
                  foo f2 = new foo();
                  f1.name = "f1:" + i.ToString();
                  f2.name = "f2:" + i.ToString();
                  this.Fooed += new FooDelegate(f1.eh1);
                  this.Fooed += new FooDelegate(f2.eh1);
            }

            [STAThread]
            static void Main(string[] args)
            {
                  foo f = new foo();
                  f.name = "f";
                  f.Fooed += new FooDelegate(f.eh1);
                  for(int i=0;i<10;i++)
                  {
                        Console.WriteLine("Loop " + (i+1).ToString());
                        f.OnFoo();
                  }
            }
      }
}


add does not appear to apply (atleast in this case) until the next time the event is called. Subtract on the other hand appears to work so long as the event has not yet been called previously.

0
 
LVL 24

Author Comment

by:Justin_W
ID: 12080161
>> add does not appear to apply (atleast in this case) until the next time the event is called.
>> Subtract on the other hand appears to work so long as the event has not yet been called previously.

Thanks Greg!

Do you have any clue as to whether that behavior is guaranteed or merely an implementation detail?  Specifically, do you know whether the behavior may be different when there are multiple Events being handled and if some of the instances are defined in VB.NET assemblies and some in C# assemblies?  I don't want to rely on ambiguous behavior, but it would be really helpful if you happen to know for sure.
0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 500 total points
ID: 12080270
as for implementational detail or guarenteed .... this is the best I could find in the C# language specification

Invocation of a delegate instance whose invocation list contains multiple entries proceeds by invoking each of the methods in the invocation list, synchronously, in order. Each method so called is passed the same set of arguments as was given to the delegate instance. If such a delegate invocation includes reference parameters (Section 10.5.1.2), each method invocation will occur with a reference to the same variable; changes to that variable by one method in the invocation list will be visible to methods further down the invocation list. If the delegate invocation includes output parameters or a return value, their final value will come from the invocation of the last delegate in the list.

I could not however find anything where they deal with adding a delegate during the invocation of a delegate.

I would assume your second case to be no ...

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 24

Author Comment

by:Justin_W
ID: 12080653
Thanks!  That helps a lot.
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 12080825
sorry I couldnt help more mate, perhaps the CLR specification would hold something ? It appears atleast at face value to be an implementational detail.
0
 
LVL 24

Author Comment

by:Justin_W
ID: 12080891
You helped a lot.  I agree about the "implementation detail" part.  Based on your test, and the C# spec., and on the way order of execution is determined for event handlers defined in different classes, it seems like semi-predictable ambiguity is the best I can hope for.  As long as it doesn't cause any runtime errors (which both our tests seem to indicate is the case), I think I can safely raise the event inside a loop (with a special exit condition) so that it keeps getting raised until the system arrives at a rest state.  Thanks again.
0
 
LVL 24

Author Comment

by:Justin_W
ID: 12424101
FYI:
I've posted a semi-related followup question here:
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/Q_21184822.html

If anyone can provide any insight on it, I'd really appreciate it.  Thanks!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question