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.
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
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.
B4X:
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
B4X:
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.");