As you have shown it, you cannot rely on the value of the flag.
Instead, use AsyncToken to keep calls sync'd with results.
Hmm, I think AsyncToken is available on UrlLoader. If not, use
HTTPService.
function load(flag:uint) {
var asToken:AsyncToken = loader.loadSomeStuff();
asToken.flag = flag;
}
function handleComplete(event:Event) {
var asToken:AsyncToken = event.token;
var sFlag:String = asToken.flag;
switch (sFlag) {
case"1":
//do 1 stuff
break;
....
}
}
Tracy
________________________________
From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of robbarreca
Sent: Monday, June 09, 2008 5:48 PM
To: [email protected]
Subject: [flexcoders] Re: Attaching custom data to events (while
avoiding race condition)
So when I say "thread" maybe I'm using the wrong terminology. I'm not
actually talking about two different users/browsers. I'm talking about
the same browser running two calls of load().
Let me expand:
function onInitialize() {
loader.addEventListener(handleComplete);
load(1); // Say this takes 20 seconds to complete load
load(2); // But this one only takes 5 seconds to complete
}
private var storedFlag:uint = 0;
function load(flag:uint) {
storedFlag = flag;
loader.loadSomeStuff();
}
function handleComplete(event:Event) {
// What is storedFlag here?
}
The execution goes like this:
load(1);
load(2);
handleComplete(event1); // Triggered from the load(1) which is the
race since this storedFlag == 2, but this is handling the completion
of the first load. I really want to get that flag data from the event
object so there is no race condition.
handleComplete(event2);
This is a simplified example, but I really do have a use case for
this. Did I do a better job of explaining this time?
-R
--- In [email protected] <mailto:flexcoders%40yahoogroups.com>
, "Daniel Gold" <[EMAIL PROTECTED]> wrote:
>
> It makes sense when dealing with concurrent programming, but Flash
is single
> threaded and you won't have a case where multiple threads are task
switching
> and simultaneously executing functions. Every function call and event
> handler in flex is basically "in-lined" and will execute to
completion.
>
> On Mon, Jun 9, 2008 at 5:11 PM, robbarreca <[EMAIL PROTECTED]>
> wrote:
>
> > Sorry, I must have not explained well. The race condition I'm
talking
> > about exists here:
> >
> > thread 1: calls load() and say someFlag gets set to 1;
> > thread 2: calls load() and someFlag is set to 2;
> > thread 1: the first load is complete and handleComplete() is called,
> > but someFlag is set to 2 when the value I want is 1.
> >
> > I want to attach a copy of the value of someFlag to the event so in
> > handleComplete() I could call event.target.someFlagCopy and always
get
> > the value that was set in *that* thread's load() call.
> >
> > Does that make any sense?
> >
> > -R
> >
> >
> > --- In [email protected]
<mailto:flexcoders%40yahoogroups.com> <flexcoders%40yahoogroups.com>,
"Daniel
> > Gold" <danielggold@> wrote:
> > >
> > > does determineFlag() do some asynch service call? If so you need
to
> > wait and
> > > raise an event or update a var in a model and listen for changes
> > that way.
> > > Otherwise your determineFlag() method will run to completion
before the
> > > loader.loadSomeStuff() line is executed, Flash is single threaded
> > for user
> > > code execution so you shouldn't have a race condition there
> > >
> > > On Mon, Jun 9, 2008 at 3:55 PM, robbarreca <rob@>
> > > wrote:
> > >
> > > > Say I have two functions load() and handleComplete(event:Event).
> > Right
> > > > now to get custom data to handleComplete I do something like
this
> > > >
> > > > private var someFlag:uint = 0;
> > > >
> > > > function load() {
> > > > loader.addEventListener(handleComplete);
> > > > someFlag = determineFlag();
> > > > loader.loadSomeStuff();
> > > > }
> > > >
> > > > function handleComplete(event:Event) {
> > > > trace(someFlag);
> > > > }
> > > >
> > > > But if I call this super fast, someFlag is gonna be wrong.
I've seen a
> > > > method where you can add an anonymous function somehow, but
I'm pretty
> > > > sure that even faced the same race condition problem. What is
the
> > > > *proper* way to go about this?
> > > >
> > > >
> > > >
> > >
> >
> >
> >
>