Looks like there is a bug in DynamicEngine. When I tried the old way, it works:

private void UserControl_Loaded(object sender, RoutedEventArgs ev)
{
    IPloader = new BackgroundWorker();
    IPloader.DoWork += new DoWorkEventHandler((s, e) =>
        {
            ScriptRuntimeSetup setup = new ScriptRuntimeSetup();
            setup.LanguageSetups.Add(Python.CreateLanguageSetup(null));
            setup.HostType = typeof(Microsoft.Scripting.Silverlight.BrowserScriptHost);
            setup.DebugMode = true;
            runtime = new ScriptRuntime(setup);
            engine = Python.GetEngine(runtime);
            foreach (string str in new string[] { "mscorlib", "System", "System.Windows", "System.Windows.Browser", "System.Net" })
            {
                runtime.LoadAssembly(runtime.Host.PlatformAdaptationLayer.LoadAssembly(str));
            }
            // engine.Execute("import imptest"); --this does not work
        }
    );
    IPloader.RunWorkerCompleted += new RunWorkerCompletedEventHandler((s, e) =>
    {
        this.textBlock1.Text = "IronPython loaded";
    }
    );
    IPloader.RunWorkerAsync();
}

What does not work is importing modules in the background thread (commented line). It throws the following exception:

System.InvalidOperationException was unhandled by user code
  Message=This operation can only occur on the UI Thread.
  StackTrace:
       at Microsoft.Scripting.Actions.Calls.MethodCandidate.Caller.Call(Object[] args, Boolean& shouldOptimize)
       at IronPython.Runtime.Types.BuiltinFunction.BuiltinFunctionCaller`5.Call4(CallSite site, CodeContext context, TFuncType func, T0 arg0, T1 arg1, T2 arg2, T3 arg3)
       at System.Dynamic.UpdateDelegates.UpdateAndExecute6[T0,T1,T2,T3,T4,T5,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5)
       at IronPython.Runtime.Importer.Import(CodeContext context, String fullName, PythonTuple from, Int32 level)
       at IronPython.Runtime.Operations.PythonOps.ImportTop(CodeContext context, String fullName, Int32 level)
       at Microsoft.Scripting.Interpreter.FuncCallInstruction`4.Run(InterpretedFrame frame)
       at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
       at Microsoft.Scripting.Interpreter.LightLambda.Run1[T0,TRet](T0 arg0)
       at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
       at IronPython.Compiler.RuntimeScriptCode.Run()
       at Microsoft.Scripting.SourceUnit.Execute()
       at Microsoft.Scripting.Hosting.ScriptSource.Execute()
       at Microsoft.Scripting.Hosting.ScriptEngine.Execute(String _expression_)
       at SLHosting.MainPage.<UserControl_Loaded>b__0(Object s, DoWorkEventArgs e)
       at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
       at System.ComponentModel.BackgroundWorker.OnRun(Object argument)
  InnerException:


runtime.ImportModule("imptest"); is similar story so I guess it cannot be done, right?

--
-- Lukáš


On 18.8.2010 5:05, Jimmy Schementi wrote:
True, but according to his exception stack it never gets there ... it throws on the "runtime = DynamicEngine.CreateRuntime(true);" line. It throws because somewhere in that method it doesn't dispatch to the UI thread as you described. Those are the fixes I need to do.

~Jimmy


2010/8/17 Dave Curylo <cury...@asme.org>
Lukas,

I think the problem is the RunWorkerCompleted handler trying to update the UI. Wrap calls from a background thread to update UI controls in something like this so the Dispatcher updates any controls on the UI thread:

this.Dispatcher.BeginInvoke(() =>
    {
        this.textBlock1.Text = "IronPython loaded";
    });

Hope that helps.

-Dave

2010/8/17 Lukas Cenovsky <cenov...@bakalari.cz>

 Hi all,
is it possible to load IronPython engine in the background thread in Silverlight?

I tried to load it via BackgroundWorker:

private void UserControl_Loaded(object sender, RoutedEventArgs ev)
{
   IPloader = new BackgroundWorker();
   IPloader.DoWork += new DoWorkEventHandler((s, e) =>
       {
           runtime = DynamicEngine.CreateRuntime(true); // debug mode true
           engine = runtime.GetEngine("python");
       }
   );
   IPloader.RunWorkerCompleted += new RunWorkerCompletedEventHandler((s, e) =>
   {
       this.textBlock1.Text = "IronPython loaded";
   }
   );
   IPloader.RunWorkerAsync();
}

It fails with the following exception:

System.UnauthorizedAccessException was unhandled by user code
 Message=Invalid cross-thread access.
 StackTrace:
      at MS.Internal.XcpImports.CheckThread()
      at System.Windows.DependencyObject.GetValueInternal(DependencyProperty dp)
      at System.Windows.Deployment.get_Parts()
      at Microsoft.Scripting.Silverlight.DynamicAppManifest.AssemblyParts()
      at Microsoft.Scripting.Silverlight.DynamicAppManifest..ctor()
      at Microsoft.Scripting.Silverlight.DynamicEngine.CreateLangConfig()
      at Microsoft.Scripting.Silverlight.DynamicEngine.CreateRuntimeSetup(Boolean debugMode)
      at Microsoft.Scripting.Silverlight.DynamicEngine.CreateRuntime(Boolean debugMode)
      at SLHosting.MainPage.<UserControl_Loaded>b__0(Object s, DoWorkEventArgs e)
      at System.ComponentModel.BackgroundWorker.OnDoWork(DoWorkEventArgs e)
      at System.ComponentModel.BackgroundWorker.OnRun(Object argument)
 InnerException:

Thanks for any advice.

--
-- Lukáš

_______________________________________________
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com


_______________________________________________
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com


_______________________________________________ Users mailing list Users@lists.ironpython.com http://lists.ironpython.com/listinfo.cgi/users-ironpython.com



_______________________________________________
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Reply via email to