[IronPython] ActiveX STA Issue in compiled Winforms EXE

Dino Viehland dinov at microsoft.com
Sat Nov 8 23:13:58 CET 2008


Oh, the problem is that pyc.py doesn't mark the thread as STA.  You can add this line to pyc.py right after mainMethod = ...

mainMethod.SetCustomAttribute(clr.GetClrType(System.STAThreadAttribute).GetConstructor(()), System.Array[System.Byte](()))

and it should work.  We should do that by default for winexe's and probably offer an option to control STA/MTA for non-WinEXEs.

Thanks for reporting this.  I've opened a bug (http://www.codeplex.com/IronPython/WorkItem/View.aspx?WorkItemId=19409) and we can probably refresh the sample.

From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Davy Mitchell
Sent: Saturday, November 08, 2008 1:59 PM
To: Discussion of IronPython
Subject: Re: [IronPython] ActiveX STA Issue in compiled Winforms EXE

Thanks Dino - here's the info:

Unhandled Exception: System.Threading.ThreadStateException: ActiveX control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be instantiated because the current thread is not in a single-threaded apartment.
   at System.Windows.Forms.WebBrowserBase..ctor(String clsidString)
   at System.Windows.Forms.WebBrowser..ctor()
   at _stub_$24##21(Closure , CallSite , CodeContext , Object )
   at Microsoft.Scripting.Actions.MatchCaller.Call2[T0,T1,TRet](Func`4 target, CallSite site, Object[] args)
   at Microsoft.Scripting.Actions.CallSite`1.UpdateAndExecute(Object[] args)
   at Microsoft.Scripting.Actions.UpdateDelegates.Update2[T,T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
   at S$1.__init__$7(Closure , Object self)
   at _stub_$18##15(Closure , CallSite , CodeContext , Object , Object )
   at Microsoft.Scripting.Actions.MatchCaller.Call3[T0,T1,T2,TRet](Func`5 target, CallSite site, Object[] args)
   at Microsoft.Scripting.Actions.CallSite`1.UpdateAndExecute(Object[] args)
   at Microsoft.Scripting.Actions.UpdateDelegates.Update3[T,T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
   at _stub_$17##14(Closure , CallSite , CodeContext , Object )
   at Microsoft.Scripting.Actions.MatchCaller.Call2[T0,T1,TRet](Func`4 target, CallSite site, Object[] args)
   at Microsoft.Scripting.Actions.CallSite`1.UpdateAndExecute(Object[] args)
   at Microsoft.Scripting.Actions.UpdateDelegates.Update2[T,T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
   at _stub_$16##13(Closure , CallSite , CodeContext , Object )
   at Microsoft.Scripting.Actions.MatchCaller.Call2[T0,T1,TRet](Func`4 target, CallSite site, Object[] args)
   at Microsoft.Scripting.Actions.CallSite`1.UpdateAndExecute(Object[] args)
   at Microsoft.Scripting.Actions.UpdateDelegates.Update2[T,T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
   at S$1.Initialize(Scope $scope, LanguageContext $language)
   at Microsoft.Scripting.Runtime.OptimizedScriptCode.InvokeTarget(LambdaExpression code, Scope scope)
   at Microsoft.Scripting.ScriptCode.Run(Scope scope)
   at IronPython.Runtime.PythonContext.CreateModule(String fileName, Scope scope, ScriptCode scriptCode, ModuleOptions options)
   at IronPython.Runtime.PythonContext.CompileModule(String fileName, String moduleName, SourceUnit sourceCode, ModuleOptions options, ScriptCode& scriptCode)
   at IronPython.Runtime.PythonContext.CompileModule(String fileName, String moduleName, SourceUnit sourceCode, ModuleOptions options)
   at IronPython.Runtime.Importer.LoadFromSourceUnit(CodeContext context, SourceUnit sourceCode, String name, String path)
   at IronPython.Runtime.Importer.LoadModuleFromSource(CodeContext context, String name, String path)
   at IronPython.Runtime.Importer.LoadFromDisk(CodeContext context, String name, String fullName, String str)
   at IronPython.Runtime.Importer.ImportFromPathHook(CodeContext context, String name, String fullName, List path, Func`5 defaultLoader)
   at IronPython.Runtime.Importer.ImportFromPath(CodeContext context, String name, String fullName, List path)
   at IronPython.Runtime.Importer.ImportTopAbsolute(CodeContext context, String name)
   at IronPython.Runtime.Importer.ImportModule(CodeContext context, Object globals, String modName, Boolean bottom, Int32 level)
   at IronPython.Runtime.Builtin.__import__(CodeContext context, String name, Object globals, Object locals, Object fromlist, Int32 level)
   at _stub_$1##1(Closure , CallSite , CodeContext , Object , String , IAttributesCollection , IAttributesCollection , PythonTuple , Int32 )
   at Microsoft.Scripting.Actions.MatchCaller.Call7[T0,T1,T2,T3,T4,T5,T6,TRet](Func`9 target, CallSite site, Object[] args)
   at Microsoft.Scripting.Actions.CallSite`1.UpdateAndExecute(Object[] args)
   at Microsoft.Scripting.Actions.UpdateDelegates.Update7[T,T0,T1,T2,T3,T4,T5,T6,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6
rg6)
   at IronPython.Runtime.Importer.Import(CodeContext context, String fullName, PythonTuple from, Int32 level)
   at IronPython.Runtime.Operations.PythonOps.InitializeModule(Assembly precompiled, String main, String[] references)
   at PythonMain.Main()

Repro code is:

import clr
clr.AddReference('System.Windows.Forms')
from System.Windows.Forms import *

class MainForm(Form):
    def __init__(self):
        self.Dock = DockStyle.Fill
        self.Controls.Add(WebBrowser())
form = MainForm()
Application.Run(form)

Compiled with:
ipy pyc.py /out:wf /target:winexe /main:repro.py

-X:ExceptionDetail didn't alter the output from the exe. Run from ipy there was no issue.

Cheers,
Davy
On Sat, Nov 8, 2008 at 5:39 PM, Dino Viehland <dinov at microsoft.com<mailto:dinov at microsoft.com>> wrote:
I'd start w/ -X:ExceptionDetail to get the full stack trace.  Presumably there's something higher up the stack then the MatchCaller on the stack.

-----Original Message-----
From: users-bounces at lists.ironpython.com<mailto:users-bounces at lists.ironpython.com> [mailto:users-bounces at lists.ironpython.com<mailto:users-bounces at lists.ironpython.com>] On Behalf Of Davy Mitchell
Sent: Saturday, November 08, 2008 6:19 AM
To: Discussion of IronPython
Subject: Re: [IronPython] ActiveX STA Issue in compiled Winforms EXE

Hi Michael

>Are you doing this from a separate thread?

No.

It's a couple of modules deep from the 'main' script. Was going to try
a single py file to repro.

> I assume it works fine when run uncompiled?

Yep, runs great.
Cheers,
Davy


On 11/8/08, Michael <fuzzyman at gmail.com<mailto:fuzzyman at gmail.com>> wrote:
> Are you doing this from a separate thread? I assume it works fine when
> run uncompiled?
>
>
> Michael
>
> http://www.ironpythoninaction.com
>
> On 8 Nov 2008, at 12:10, "Davy Mitchell" <daftspaniel at gmail.com<mailto:daftspaniel at gmail.com>> wrote:
>
>> IPY 2.0 RC1
>>
>>
>> ActiveX Issue in a pyc compiled Winforms EXE which uses the
>> WebBrowser control.
>>
>> Unhandled Exception: System.Threading.ThreadStateException: ActiveX
>> control '8856f961-340a-11d0-a96b-00c04fd705a2' cannot be
>> instantiated because the current thread is not in a single-threaded
>> apartment.
>>    at System.Windows.Forms.WebBrowserBase..ctor(String clsidString)
>>    at System.Windows.Forms.WebBrowser..ctor()
>>    at _stub_$643##332(Closure , CallSite , CodeContext , Object )
>>    at Microsoft.Scripting.Actions.MatchCaller.Call2[T0,T1,TRet]
>> (Func`4 target, C
>>
>> Help!! :-)
>>
>> Any thoughts before I delve further into this?
>>
>> Thanks
>> Davy Mitchell
>> _______________________________________________
>> Users mailing list
>> Users at lists.ironpython.com<mailto:Users at lists.ironpython.com>
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com<mailto:Users at lists.ironpython.com>
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
_______________________________________________
Users mailing list
Users at lists.ironpython.com<mailto:Users at lists.ironpython.com>
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

_______________________________________________
Users mailing list
Users at lists.ironpython.com<mailto:Users at lists.ironpython.com>
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20081108/f05e7ca2/attachment.html>


More information about the Ironpython-users mailing list