debugging Flex event flow - apache-flex

I'm stepping through my code to figure out why a certain function takes more time to run the first time it gets called than on successive calls. The code flow for each function call is the same up to when a dispatchEvent gets called. I'm pretty sure it's different afterwards, as that call takes a lot more time the first time around. Unfortunately, I have no idea which other parts of the code chew on this specific event and thus cannot step through the handling of such event.
The question: is there a way to either figure out who handles such events or magically step through the handling code without explicitly setting breakpoints there?
thank you!

Not in an easy fashion, no. It's a pro and con of Flash (or any event based framework). You don't know when it's fired, where it's fired from (think bubbling), or who is listening for it. But at the same time, anyone could listen for any event, from anywhere (so long as it's within the display tree).
Normally what I do is just do a workspace search (ctrl+H in Flash Builder) and search for that specific event (you should be using static constants for dispatching/listening event types) and see who's doing what.

Related

SignalR and SqlDependency refreshment issue - ASP.NET

I have a table in MSSQL database, and I have an ASPX page, I need to push all new rows to the page in a descending order. I found this awesome tutorial which is using SignalR and SqlDependency and it shows only the last row descarding the previous rows which have been added when I'm online, it does that because it has a span element to show data and every time it overwrites this span, so I modified the JavaScript code to append the new data and it works fine.
The problem now is when I refreshed the page for the first time, I'll get the new rows twice, and if I refreshed the page again I'll get the new rows triple .. and so on.
The only solution is to close the application and reopen it again, it looks like reset the IIS.
So, what can I do to avoid duplicating data in the online show?
It is not a SignalR issue, that happens because the mentioned tutorial has a series of mistakes, the most evident being the fact that it continuously creates SqlDependency instances but then it trashes them without never unsubscribing from the OnChange event. You should start by adding something like this:
SqlDependency dependency = sender as SqlDependency;
dependency.OnChange -= dependency_OnChange;
before calling SendNotifications inside your event handler. Check this for some inspiration.
UPDATE (previous answer not fully accurate but kept in place for context)
The main problem here is that this technique creates a sort of auto-regenerating infinite sequence of SqlDependencies from inside instances of Web Forms pages, which makes them unreachable as soon as you page has finished rendering. This means, once your page lifecycle is complete and the page is rendered, the chain of dependencies stays alive and keeps on working even if the page instance which created has finished its cycle. The event handler also keeps the page instance alive even if unreachable, causing a memory leak.
The only way you can control this is actually to generate these chains somewhere else, for example within a static type you can call passing some unique identifier (maybe a combination of page name and username? that depends on your logic). At the first call it will do what currently happens in your current code, but as soon as you do another call with the same parameters it will do nothing, hence the previously created chain will go on being the only one notifying, with no duplicate calls.
It's just a suggestion, there would be many possible solutions, but you need to understand the original problem and the fact that it is practically impossible to remove those chains of auto-regenerating dependencies if you don't find a way to keep track of them and create them only when necessary. I hope that part is clear.
PS: this behavior is very similar to what you get sometimes with event handlers getting leaked and keeping alive objects which should be killed, this is what fooled me with the previous answer. It is in a way a similar problem (leaked objects), but with a totally different cause. The tutorial you follow does not clarify that and brings you to this situation where phantom code keeps on executing and memory is lost.
I got it, although I don't like this way absolutely, I have declared a static member in the Global.asax file and in the Page_Load event I checked its value, if it was true don't run a new instance of SqlDependency, otherwise run it.
if (!Global.PageIsFired)
{
Global.PageIsFired = true;
SqlDependency.Stop(ConfigurationManager.ConnectionStrings["db"].ConnectionString);
SqlDependency.Start(ConfigurationManager.ConnectionStrings["db"].ConnectionString);
SendNotifications();
}
Dear #Wasp,
Your last update helped me a lot to understand the problem, so thank you so much for your time and prompt support.
Dear #dyatchenko,
Thanks a lot for your comments, it was very useful too.

Using setFire, setAhead, etc, without calling execute()

I'm extending an open-source AdvancedRobot in Robocode. That robot uses setFire to shoot, but never calls execute (doesn't appear in the code). I'm wondering how it's possible to still be able to shoot (it does). SetFire's doc says : This call returns immediately, and will not execute until you call execute() or take an action that executes.
I have no idea what "take an action that executes" mean.
Even better, what does "action" mean ?
My main goal was to do something every time a bullet is fired, so I have overridden the fire and fireBullet methods, but that doesn't work with the "set" methods (since it's possible to call it several times, ovveriding the previous order each time and shooting only when you "call execute() or take an action that executes"). So, maybe there is a way around.
Whatever, I'd be glad if anyone could help on any of those concerns.
Thank you very much.
This question sure is old, but for future reference:
Actions that execute are basically "stuff the robot can do that doesn't start with set", like fire or ahead, and so on. Calling any of these will also execute.
If you want to do something special every time a bullet is fired, you can use the following:
if (setFireBullet(someBulletPower) != null) {
// you only land here when a REAL bullet is fired,
// that is, when the gun heat was down.
}
Of course this only works if the open source bot you're extending is executing (it seems to be doing that, though I can't be sure if it is doing this every turn without knowing the code).

How do events work in Qt?

I have some questions about the general usage of Qt events. I'm new to Qt, and I am going to try out making a custom event. The questions I ask are related to this article: Qt 4.8: The Event System
When does an event “occur”? In Qt's built in events, mouse clicks and key presses are put into a queue and then the functions are executed at the next frame. I’m especially wondering about this for custom events, because I’m not sure if it always occurs when a mouse is clicked. For custom events, is it when you send the event into the queue and then waits to be processed by the event() function at the next frame?
To create a custom event, must you make a class that inherits from QEvent and register an event with registerEventType() function? Is this the standard process, or can an event simply be a class? How does this function, registerEventType(), work?
Do custom events have a QEvent::Type? Is this the number between 1000 and 6563 that is given when using the registerEventType() function?
What is the recipient of an event? It seems to be in functions' parameters like postEvent(), sendEvent(), etc. However, I am not sure what this object is for.
Where is the actual code that is executed when an event is fired?
Is it a function, or is it in the event() function of QObjects.
Also any working examples of Qt events (both built-in or custom) in action would be helpful.
You should look at Qt Examples online or in QtSDK, there are tons of them.
IMO you didn't search hard.
Ad.1. It occurs after you fire it with QApplication::postEvent() or QApplication::sendEvent(). Not immediately of course, because it has to go through the main loop, etc. The order of events should be preserved, though.
Ad.2. Look at this, second anwser.
Ad.3. Go to Ad.2.
Ad.4. This object will receive this custom event in QObject::customEvent() handler.
Ad.5. Go to Ad.4.

Preventing timers from running when something is already busy (using Qt)

In my mathematical application I am using timers to regularly perform certain actions. These actions can also be configured by my users. Now I don't want these actions to be executed if there is already another action busy.
E.g. if the user just started a complex calculation by selecting a menu entry, I don't want to execute the actions behind my timers.
Problem is that the user can execute an action via a lot of different ways (via the menu, by clicking somewhere, via popup menu, via drag-and-drop, ...). What I effectively want is to prevent the timers from going off if the application is currently not in the main event loop.
I will give a more concrete example to make it clearer:
At startup I create the timers
If a timer goes off, I execute some actions which, in practice, could access almost every bit in may application's data structure.
Now suppose the user starts a mathematical algorithm (via the menu, by clicking or by dragging elements on the screen, it doesn't matter how he started it).
The algorithm will perform lots of calculations (in the main thread). Since they are executed in the main thread, the timer events will not go off.
Now the algorithm shows a message box (could be a warning or a question).
While the message box is open, events are processed again, including my timer events, which could possibly perform incorrect calculations because there is already another algorithm running.
Reworking my application so that I move logic to a separate worker thread, or adding checks to all of my actions isn't possible at this moment. So please don't suggest to completely rework my application.
What I tried so far is the following:
Using postEvent to send an event, hoping that this event would only be executed in the main event loop. Unfortunately, also the message box's event loop seems to process posted events.
Using the QEvent::WindowBlocked and QEvent::WindowUnblocked events to see when a modal dialog was opened. In my timer-event-logic I can check whether we are between QEvent::WindowBlocked-QEvent::WindowUnblocked calls or not. Unfortunately, these events only work for modal dialogs created by Qt itself, not for other dialogs (e.g. the Windows MessageBox, or the system's printer configuration dialog). Also, this trick would not help if there would be other event loops created by sub routines.
What I actually need to solve my problem is a simple function, that:
If the application is handling an event in the main event loop returns true
If the application is handling an event in another [sub] event loop, it returns false
An alternative could be to return a level that indicates the 'depth' of the handled event.
Anyone suggestions?
You could hook into the event loop of your main thread/application using QAbstractEventDispatcher. Conditionaly filter out QTimer-events based on your application state.

ActionScript: pushing a closure onto the event stack?

On occasion, I have wanted to push a closure onto ActionScript's event stack so that it will be executed after the current event handler. Right now I use setTimeout(closure, 0). Is there a more direct way of doing this?
setTimeout(closure, 0) is creating a new event stack. I don't understand your objective if this solution isn't working for you. What is the goal you're trying to accomplish?
Flex has ENTER_FRAME events, Timer, callLater, setTimeout, setInterval, all which delay calls and create new execution stacks.
Are you trying to inject code into the current stack? If so, you might need to look at something like this: http://en.wikipedia.org/wiki/Active_object. The idea being that you push functions (closures) into an array, and the active object controller pulls the next one off the list when the previous one has run to completion. That's the simplest case. You can write a more complicated one that will have priority stacks like high, medium, low, with your own schedule management system. (e.g., low get promoted after waiting too long).
But hey! The devil is in the details. What's the goal?
Take a look at capture and bubbling phases of the as3 events.
I found this nice chapter that explains clearly the proccess: http://books.google.com/books?id=yFNZGjqJe9IC&lpg=PA250&ots=oPB9HXIby7&dq=flash%20event%20bubbling%20phase&pg=PA250#v=onepage&q=&f=false
And also check the EventDispatcher class documentation that explain the use of this different phases.

Resources