Here is the problem: I want to write a function that makes an outside
communication with a web server and exhibits to the caller blocking bahavior.
That is, I call something like getUserStatus(userid), and it either returns
with the value I want from my database (ColfFusion page), or after some
reasonable amount of time, if I'm not able to communicate with that page, it
returns a special value indicating so.
As best as I can tell, what is happening behind the scenes for a LoadVars
object is it is essentially forking a new thread to handle this communication
asynchronously from what the thread that invoked LoadVars.sendAndLoad() is
doing.
ActionScript, being a programming language for non-programmers, makes no
mention whatsoever of threads, nor do I see any way to call a sleep function or
a yeild function on the current thread (i.e., some way to tell 'this' thread to
just chill for a sec and let other threads do what they need to do).
There is the setInterval() function, but again this appears to simply be
forking another thread to periodically call another callback - the code that is
calling setInterval() itself essentially returns immediately and then proceeds
to do whatever it is doing. But what I want to do next depends on the value I
am trying to read.
So, I figured, "Fine!" I'll just busy wait until my callback has done what it
needs to do. But the problem is that my busy waiting seems to be blocking my
callback from doing what it needs to do.
Below is my attempt at a blocking function. On the stage I simply have a bunch
of dynamic text fields that I set the values of to see what is going on.
Regardless of what I set the timeout value to, shortly after that that
result_lv.onLoad gets called, I get back a 200 HTTP status, and my expected
value is returned from ColdFusion. Unfortunately, my main thread has already
seen the timeout by then.
So... I'm not seeing how to get this to work right. This must be a common
problem people are handling some other way - can someone help me out?
// Wouldn't it be nice if Flash's editor were smart enough to auto-indent
// with spaces instead of tabs? Then this code would look right when I
// paste it.
function getLessonStatus(userid, courseid):Object
{
rv_obj = new Object(); // return value Object
// what to do after communication completes...
var result_lv:LoadVars = new LoadVars();
result_lv.onLoad = function(success:Boolean) {
rv_obj.success = success;
loaded_at_ta.text = getTimer();
if (success) {
// append 'status' attribute as found in
tblStudentProfile
rv_obj.status = result_lv.status;
}
success_ta.text = rv_obj.success;
status_ta.text = rv_obj.status;
};
result_lv.onHTTPStatus = function(http_return_code:Number) {
http_status_ta.text = http_return_code;
};
// make HTTP request
var send_lv:LoadVars = new LoadVars();
send_lv.userid = userid;
send_lv.courseid = courseid;
text0.text = "Talking to DB...";
text2.text = LESSON_STATUS_URL;
send_lv.sendAndLoad(LESSON_STATUS_URL, result_lv);
// I want this function to have blocking behavior, but the communication
// via LoadVars is inherently non-blocking (i.e., callback based). Wait
// until either TIMEOUT has expired or rv_obj has been updated.
var intervalId:Number;
var interval:Number = 100; // msec
var timeout = 2 * 1000; // msec
var start_time:Number = getTimer();
start_time_ta.text = start_time;
var now:Number = getTimer();
// busy wait
N = 0;
while ((rv_obj.success == undefined) && (now - start_time < timeout)) {
now = getTimer();
now_ta.text = now;
N_ta.text = N;
N++;
}
// OK, did we timeout or is the object updated?
if (now - start_time >= timeout) {
// TIMED OUT!!
rv_obj = new Object();
rv_obj.success = false;
rv_obj.timeout = true;
text3.text = "TIMED_OUT!";
}
else {
rv_obj.timeout = false;
}
return rv_obj;
} // function getLessonStatus(userid, courseid):Object
var obj:Object = getLessonStatus('some_user_id', 'some_course_id');
success_ta.text = obj.success;
status_ta.text = obj.status;
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com