Real or Simulated Background Threading

General Discussion of Scripting for Flex, Flash & CS SDK

Moderators: Tom, Kukurykus

AaronPowers

Real or Simulated Background Threading

Post by AaronPowers »

I'm working on a Flex & JavaScript panel to run in Adobe Photoshop. Some actions (specifically, xtool's Stdlib.getLayerList) can take a very long time to process, e.g. in some tests I've seen it take up to 15 seconds.

So, what are the best ways to simulate or achieve a background thread without blocking the UI?

I have a couple ideas in how to simulate or actually do multithreading. I'm planning to try these out, but it's hard to tell what will work best. Here's my ideas:

(a) Using raw JavaScript seems to block the Photoshop UI, which doesn't work as a background. Additionally, there's no "setTimeout()" method like HTML's JavaScript.
(b) It looks like the Flex panels created in Adobe Flash Builder run in their own thread -- is this true? It's clear, however, that as soon as it calls JavaScript, it looks like it blocks the Photoshop UI. I could break the JSX down into short chunks that are less than 100ms each, and call them consecutively from ActionScript, but I think this would still, in theory, block the UI for brief periods of time.
(c) The CS SDK be used in CS5+ to call Photoshop commands straight from ActionScript, skipping JSX -- will this block the UI as much, and if so, will we still need to do threading? This is only even theoretically possible in CS5+. So if it worked, would it be possible to do the smarter thing in CS5+ but in CS4 fall back to using the slow JSX approach?

I'll post my findings here as I test these out -- if you can see any alternatives or know about the tradeoffs of threading and simulated threading, this would be a great place to post.

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

AaronPowers

Real or Simulated Background Threading

Post by AaronPowers »

I ran a quick test and it appears that ActionScript threads do not run in a separate thread.

So the bad news is that (b) and (c) won't work.

However, it looks like there's another approach that can work:
(d) Use ActionScript's Timer to run a thread that sleeps every once in a while, and have a worker function that does small chunks of work. Here's the basic idea:

public function startRunning():void
{
var timer:Timer= new Timer(100);
timer.repeatCount=0;
timer.addEventListener(TimerEvent.TIMER,doSomeWork);
timer.start();
}
private var i:Number=0;
public function doSomeWork(event:TimerEvent):void {
// do just a little bit of work here, for example:
statusField.text=""+i++;
}
AaronPowers

Real or Simulated Background Threading

Post by AaronPowers »

Another somewhat useful bit of news from my test, that doesn't apply directly to threading: You can include the CS SDK even when you're using Photoshop CS4, and nothing will go wrong until you make a call to the CS SDK. That means you can build a plugin that has the CS SDK in it, take advantage of it for CS5+, but if you avoid using those commands in CS4 your plugin will still work.