Weird timeout issue
Posted: Mon Dec 07, 2009 10:50 pm
The process I am having trouble with is this:
- SWF communicates with .NET code via ExternalInterface.call
- .NET code does something time consuming (image processing, in my particular situation.... but it doesn't matter)
- After processing, .NET code calls FlashMethod_CallFunction to call the SWF to take some action
If the .NET code takes too long, the call to FlashMethod_CallFunction is never acknowledged in the SWF.
I boiled it down to a simple test sample:
.NET Code
And the SWF code (note, you need to create a button and add it to the stage... call it "btn")
Now, comment out the Thread.Sleep call so it returns immediately. You will find it works properly.
Uncomment the code, and the final result is never shown in the SWF. If I debug through the .NET code, it calls FlashMethod_CallFunction, but something happens behind the scenes to cause it to fail.
- SWF communicates with .NET code via ExternalInterface.call
- .NET code does something time consuming (image processing, in my particular situation.... but it doesn't matter)
- After processing, .NET code calls FlashMethod_CallFunction to call the SWF to take some action
If the .NET code takes too long, the call to FlashMethod_CallFunction is never acknowledged in the SWF.
I boiled it down to a simple test sample:
.NET Code
Code: Select all
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace FInBoxTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
f_in_box__control1.FlashProperty_Movie = [your swf file name];
f_in_box__control1.FlashMethod_Play();
}
private void f_in_box__control1_OnFlashCall(object sender, string request)
{
XmlDocument document = new XmlDocument();
document.LoadXml(request);
XmlAttributeCollection attributes = document.FirstChild.Attributes;
string command = attributes.Item(0).InnerText;
if (command == "doCommunicate")
{
// pretend I'm doing something time consuming
System.Threading.Thread.Sleep(30000);
string args = "<arguments><string>done</string></arguments>";
string func = "<invoke name=\"communicationComplete\" returntype=\"xml\">" + args + "</invoke>";
f_in_box__control1.FlashMethod_CallFunction(func);
}
}
}
}
And the SWF code (note, you need to create a button and add it to the stage... call it "btn")
Code: Select all
import flash.external.*;
// wire up callback for f-in-box to swf communication
ExternalInterface.addCallback("communicationComplete", communicationComplete);
// add a button to the stage, call it "btn" and wire up the click
btn.addEventListener(MouseEvent.CLICK, doCommunicate);
// receive response from .NET
function communicationComplete(arg){
var tf:TextField = new TextField();
tf.text = arg;
this.addChild(tf);
}
// talk to .NET
function doCommunicate(evt){
if(ExternalInterface.available){
ExternalInterface.call("doCommunicate");
}
}
stop();
Now, comment out the Thread.Sleep call so it returns immediately. You will find it works properly.
Uncomment the code, and the final result is never shown in the SWF. If I debug through the .NET code, it calls FlashMethod_CallFunction, but something happens behind the scenes to cause it to fail.