Flex: Looking for design pattern to display busy c

2019-08-25 01:14发布

问题:

I have a Flex 4 app that now and then needs to do a lot of processing, which causes a user to wait a few seconds for it to complete. I know Flex allows one to set and remove busy cursors via the cursor manager. I'm using it as follows:

CursorManager.setBusyCursor();    // add busy cursor
// execute lengthy processing here; e.g. switch to a new screen with a lot of layout
CursorManager.removeBusyCursor(); // remove busy cursor

However, in practice, for certain situations, the busy cursor doesn't display, or, if it displays, it displays just before it gets removed (if you blink you'd miss it). The idea is to have the busy cursor display while the lengthly processing occurs, not for a fraction of a second after it completes.

So, I'm wondering if there's a design pattern I can use to make sure the busy cursor is always displayed BEFORE executing the lengthly processing steps. For example, these processing steps could be: (1) transitioning to a new screen that must be built, which has a complex layout, or (2) creating a chart that takes a long time to render, etc.

I suspect anyone designing in Flex has run into this at one time or another. Is there a general design pattern one can use, or is it a unique adventure each time to figure out where exactly to execute the busy cursor so that it displays at the right moment in time? Any advice appreciated.

回答1:

Your problem is that any kind of "lengthy processing" will freeze the UI, so that it will not get a chance to update the screen - and therefore not show any cursor changes prior to starting the calculation.

There are a couple of thing you can do, but before we get into details: Of course it would be best to keep freezes from happening in the first place! You can do this in one of two ways: Change your calculations in such a way that you either optimize your algorithms enough so that they can complete faster than the current frame rate, or break down longer calculations into smaller chunks, thus "spreading" resource intensive loops across multiple frames - and allow the screen to update in between. You can use workers for that in the current FP version, or pseudo threads in older ones.

For quick results, just delay the heavy process by 100ms using setTimeout, after changing the cursor. That way, the screen can update before the calculations start.