Event difference between IDE and optimised

Discussion in 'Beta Versions' started by agraham, Aug 30, 2008.

  1. agraham

    agraham Expert Licensed User

    You will probably say this is illegal but the attached B4ppc code raises one button click event in the IDE and two when optimised compiled.
    Code:
    Sub Globals
       
    'Declare the global variables here.
    End Sub

    Sub App_Start
       Form1.Show
       AddEvent(
    "button1", click, "Click")
    End Sub

    Sub Button1_Click
    End Sub

    Sub Click
       
    Msgbox(Sender & " - click")
    End Sub
    The reason being that the use of a hashtable to connect events to controls assumes only one event per control action exists. Here the existing event delegate isn't removed from the control so that gets called as well as the new one and both are vectored to the same sub. To handle Thread events in my FormEx I removed the original events by
    Code:
    EventInfo ei = threadobj.GetType().GetEvent("ThreadEvent", BindingFlags.IgnoreCase | BindingFlags.Public
        | BindingFlags.Static | BindingFlags.Instance);
    if (ei != null)
    {
        // can only get the delegates via a Field access
        FieldInfo field = threadobj.GetType().GetField(
    "ThreadEvent", BindingFlags.NonPublic | BindingFlags.Instance);
        EventHandler eh = field.GetValue(threadobj) 
    as EventHandler;
        //ei.RemoveEventHandler(threadobj,dels[
    0]); //could remove a single event
        MethodInfo mi = ei.GetRemoveMethod();
        foreach (Delegate del 
    in eh.GetInvocationList()) // remove all
            ei.GetRemoveMethod().Invoke(threadobj, new object[] { del 
    });
        ei.AddEventHandler(threadobj, new EventHandler(this.ThreadEvent));
    }
    else
        throw new Exception("Event does not exist in object.");
    I did that as the event handler was different. In this case, as there is a common event handler the AddEventHandler() could be skipped if an event already exists.
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    That is indeed an illegal code. Basic4ppc events model allows only one event per control (unlike the .Net events which are MulticastDelegates).

    There are very few situations that require assigning several listeners to a single control event.
     
  3. agraham

    agraham Expert Licensed User

    Yes, I already said that.
    Sorry but that wasn't the point I was trying to make. I was trying so say that as B4ppc only allows one event Sub per control event why not allow AddEvent to cleanly change that event if required in the optimised compiler (as it seems to do in the IDE) which wouldn't be difficult to do.
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    AFAIK there is no simple way to remove all event handlers.
    The optimized compiler uses a more straightforward method to handle events. However unlike the IDE interpreter it is not simple to exchange the events.

    Edit: Now I understand your code...
     
  5. agraham

    agraham Expert Licensed User

    It's in that code above! ( or below now :) )
    Code:
    // can only get the delegates via a Field access
        FieldInfo field = threadobj.GetType().GetField(
    "ThreadEvent"
               BindingFlags.NonPublic | BindingFlags.Instance);
        EventHandler eh = field.GetValue(threadobj) 
    as EventHandler;
        //ei.RemoveEventHandler(threadobj,dels[
    0]); //could remove a single event
        MethodInfo mi = ei.GetRemoveMethod();
        foreach (Delegate del 
    in eh.GetInvocationList()) // remove all
            ei.GetRemoveMethod().Invoke(threadobj, new object[] { del 
    });
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    I will check it...
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice