Any chance you can share what the source is? The exception comes from PythonCopyReg&#39;s initialization, which means we might have a bug (it might not be thread-safe, or something else). If you could create a minimal sample that has the issue that would be even better.<div>
<br></div><div>- Jeff<br><br><div class="gmail_quote">On Thu, Apr 14, 2011 at 9:18 PM, surangika ranathunga <span dir="ltr">&lt;<a href="mailto:lady_radsu@yahoo.com">lady_radsu@yahoo.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<table cellspacing="0" cellpadding="0" border="0"><tbody><tr><td valign="top" style="font:inherit">Hi,<br>Below is the exception I receive:<br>here, D:\PhD Work\Research\libomv\test\MonitorService\MonitorService\ExpectationMonitor.cs:line 88 refers to source.Execute(scope); in the below given code.<br>
<br>System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---&gt; System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---&gt; System.NullReferenceExce<br>
ption: Object reference not set to an instance of an object.<br>   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)<br>   at System.Collections.Generic.Dictionary`2.set_Item(TKey key, TValue value)<br>
   at IronPython.Runtime.PythonContext.GetEqualSite(Type type)<br>   at
 IronPython.Runtime.CommonDictionaryStorage.UpdateHelperFunctions(Type t, Object key)<br>   at IronPython.Runtime.CommonDictionaryStorage.AddNoLock(Object key, Object value)<br>   at IronPython.Runtime.CommonDictionaryStorage.Add(Object key, Object value)<br>
   at IronPython.Runtime.PythonDictionary.SetItem(Object key, Object value)<br>   at IronPython.Runtime.PythonDictionary.set_Item(Object key, Object value)<br>   at IronPython.Modules.PythonCopyReg.PerformModuleReload(PythonContext context, PythonDictionary dict)<br>
   --- End of inner exception stack trace ---<br>   at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct&amp; sig, MethodAttributes methodAttributes, RuntimeType typeOwner)<br>
   at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments,
 Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner)<br>   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)<br>
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)<br>   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)<br>
   at IronPython.Runtime.PythonContext.CreateBuiltinModule(String moduleName, Type type, ModuleOptions options)<br>   at IronPython.Runtime.PythonContext.CreateBuiltinModule(String moduleName, Type type)<br>   at IronPython.Runtime.PythonContext.CreateBuiltinModule(String name)<br>
   at IronPython.Runtime.PythonContext.GetBuiltinModule(String name)<br>   at
 IronPython.Runtime.Importer.ImportBuiltin(CodeContext context, String name)<br>   at IronPython.Modules.PythonCopyReg.EnsureModuleInitialized(CodeContext context)<br>   at IronPython.Modules.PythonCopyReg.GetDispatchTable(CodeContext context)<br>
   at IronPython.Modules.PythonRegex.PerformModuleReload(PythonContext context,PythonDictionary dict)<br>   --- End of inner exception stack trace ---<br>   at Microsoft.Scripting.Actions.Calls.MethodCandidate.Caller.Call(Object[] args, Boolean&amp; shouldOptimize)<br>
   at IronPython.Runtime.Types.BuiltinFunction.BuiltinFunctionCaller`5.Call4(CallSite site, CodeContext context, TFuncType func, T0 arg0, T1 arg1, T2 arg2, T3 arg3)<br>   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)<br>
   at IronPython.Runtime.Importer.Import(CodeContext
 context, String fullName, PythonTuple from, Int32 level)<br>   at IronPython.Runtime.Operations.PythonOps.ImportTop(CodeContext context, String fullName, Int32 level)<br>   at Microsoft.Scripting.Interpreter.FuncCallInstruction`4.Run(InterpretedFrame frame)<br>
   at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)<br>   at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)<br>   at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)<br>
   at IronPython.Compiler.PythonScriptCode.Run(Scope scope)<br>   at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)<br>   at IronPython.Compiler.RuntimeScriptCode.Run(Scope scope)<br>   at Microsoft.Scripting.SourceUnit.Execute(Scope scope, ErrorSink errorSink)<br>
   at Microsoft.Scripting.SourceUnit.Execute(Scope scope)<br>   at
 Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope)<br>   at MonitorService.ExpectationMonitor.Initialize(String rule) in D:\PhD Work\Research\libomv\test\MonitorService\MonitorService\ExpectationMonitor.cs:line 88<br>
<br>--- On <b>Thu, 14/4/11, Jeff Hardy <i>&lt;<a href="mailto:jdhardy@gmail.com" target="_blank">jdhardy@gmail.com</a>&gt;</i></b> wrote:<br><blockquote style="border-left:2px solid rgb(16, 16, 255);margin-left:5px;padding-left:5px">
<br>From: Jeff Hardy &lt;<a href="mailto:jdhardy@gmail.com" target="_blank">jdhardy@gmail.com</a>&gt;<br>Subject: Re: [IronPython] how to generate multiple concurrent scriptign engines?<br>To: &quot;Discussion of IronPython&quot; &lt;<a href="mailto:users@lists.ironpython.com" target="_blank">users@lists.ironpython.com</a>&gt;<br>
Cc: &quot;surangika ranathunga&quot; &lt;<a href="mailto:lady_radsu@yahoo.com" target="_blank">lady_radsu@yahoo.com</a>&gt;<br>Received: Thursday, 14 April, 2011, 7:35 AM<div><div></div><div class="h5"><br><br><div>As long as you have a ScriptScope per thread, you should be able to<br>
share the ScriptEngine &amp; Runtime between threads.<br><br>What is the exception that you are getting? If it&#39;s a GUI
 program,<br>remember that UI elements can only be manipulated from the main<br>thread.<br><br>- Jeff<br><br>On Thu, Apr 14, 2011 at 1:02 AM, surangika ranathunga<br>&lt;<a href="http://mc/compose?to=lady_radsu@yahoo.com" target="_blank">lady_radsu@yahoo.com</a>&gt; wrote:<br>
&gt;<br>&gt; Hi,<br>&gt; I am using Ironpython to connect to a legacy python code from C#.<br>&gt; I am not fully familiar with Ironpython, but managed to get it working for a single-threaded application.<br>&gt; This is how I implemented this:<br>
&gt;<br>&gt;                 ScriptEngine engine = Python.CreateEngine();<br>&gt;                 ScriptRuntime runtime = engine.Runtime;<br>&gt;                 ScriptScope scope =
 runtime.CreateScope();<br>&gt;                 ops = engine.Operations;<br>&gt;                 ScriptSource source = engine.CreateScriptSourceFromFile(&quot;ironpython.py&quot;);<br>&gt;                 source.Execute(scope);<br>
&gt;                 string class_param = rule;<br>&gt;                 object[] parameters = new object[1];<br>&gt;                 parameters[0] = class_param;<br>&gt;                 object klass =
 scope.GetVariable(&quot;model_checker&quot;);<br>&gt;                 object instance = ops.Invoke(klass, parameters);<br>&gt;                 method = ops.GetMember(instance, &quot;check_model&quot;);<br>&gt;<br>&gt; Now I want to make my application multi-threaded, and I want to have concurrently running scripting engines. Essentially, these engines should access the same underlying python code, and hence these scripting engines are identical to each other.<br>
&gt; Without any change, the above code works fine for most of the time for a multi-threaded application. However, there is an exception that appears from time to time, and I suspect that it is due to a concurrency issue.<br>
&gt;<br>&gt; Is there anything specific that I should do to generate multiple scripting engines running in separate threads without interfering
 with each other?<br>&gt;<br>&gt; any ideas greatly appreciated<br>&gt;<br>&gt; _______________________________________________<br>&gt; Users mailing list<br>&gt; <a href="http://mc/compose?to=Users@lists.ironpython.com" target="_blank">Users@lists.ironpython.com</a><br>
&gt; <a href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com" target="_blank">http://lists.ironpython.com/listinfo.cgi/users-ironpython.com</a><br>&gt;<br></div></div></div></blockquote></td></tr></tbody></table>
</blockquote></div><br></div>