Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4ppc (Windows Mobile) > Beta Versions
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Beta Versions This forum is the place to discuss issues regarding Basic4ppc beta versions.

Event difference between IDE and optimised

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-30-2008, 11:13 AM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 5,953
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default Event difference between IDE and optimised

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.
Reply With Quote
  #2 (permalink)  
Old 08-30-2008, 01:52 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 13,162
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

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.
Reply With Quote
  #3 (permalink)  
Old 08-30-2008, 02:05 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 5,953
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

Quote:
Originally Posted by Erel View Post
Basic4ppc events model allows only one event per control (unlike the .Net events which are MulticastDelegates).
Yes, I already said that.
Quote:
There are very few situations that require assigning several listeners to a single control event.
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.
Reply With Quote
  #4 (permalink)  
Old 08-30-2008, 02:20 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 13,162
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

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...
Reply With Quote
  #5 (permalink)  
Old 08-30-2008, 02:26 PM
agraham's Avatar
Basic4ppc Expert
 
Join Date: Jul 2007
Location: Cheshire, UK
Posts: 5,953
Awards Showcase
Innovator medal Beta Tester Forum Contributer 
Total Awards: 3
Default

Quote:
Originally Posted by Erel View Post
AFAIK there is no simple way to remove all event handlers..
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 });
Reply With Quote
  #6 (permalink)  
Old 08-30-2008, 02:43 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 13,162
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

I will check it...
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Optimised compiler bug using ^ on control.text agraham Bug Reports 0 07-28-2008 04:29 PM
cTicksPerDay: optimised compilation error with timer LineCutter Bug Reports 2 04-21-2008 07:15 PM
What's The Difference? Louis Chit Chat 1 03-08-2008 09:38 AM
V6-not-optim, V6-Debug, and V6-optimised give not the same nested FOR-NEXT execution dan kabestan Questions (Windows Mobile) 1 01-04-2008 08:17 PM
Difference Between Two Dates dvanwig Questions (Windows Mobile) 7 08-02-2007 05:29 AM


All times are GMT. The time now is 04:21 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0