<HTML>
<HEAD>
<TITLE>Re: [Python.NET] dotnet, threading and py2exe</TITLE>
</HEAD>
<BODY>
<FONT FACE="Verdana, Helvetica, Arial"><SPAN STYLE='font-size:12.0px'>wow, python.net, pythoncom and py2exe. Just need to find a way to import <BR>
IronPython and use something from that too... ;)<BR>
<BR>
-Brian<BR>
<BR>
<BR>
On 1/3/08 10:55 AM, "Maksim Kozyarchuk" <mkozyarchuk@funddevelopmentservices.com> wrote:<BR>
<BR>
</SPAN></FONT><BLOCKQUOTE><FONT FACE="Verdana, Helvetica, Arial"><SPAN STYLE='font-size:12.0px'>We found that the following code as the first line of your python file puts you into the STA.<BR>
<BR>
import pythoncom<BR>
pythoncom.CoInitialize()<BR>
<BR>
Maksim<BR>
<BR>
On 1/3/08 9:44 AM, "Brian Lloyd" <brian.lloyd@revolutionhealth.com> wrote:<BR>
<BR>
</SPAN></FONT><BLOCKQUOTE><FONT FACE="Verdana, Helvetica, Arial"><SPAN STYLE='font-size:12.0px'>Hi Sebastian - when you talk about it 'running perfectly with<BR>
the normal python.exe', I assume you mean the one that comes<BR>
with python.net.<BR>
<BR>
That exe is actually written in C# and explicitly sets the<BR>
apartment mode to STA.<BR>
<BR>
I suspect the problem is that py2exe will create its own form<BR>
of exe which won't do that, so you'll get the default threading<BR>
model (MTA).<BR>
<BR>
It might be possible for your script to set the main thread to<BR>
STA, if it is the very first thing your program does. You may<BR>
want to refer to the docs on that -- I remember it was kind of<BR>
complicated and hard to get it set from python code, but don't<BR>
recall all of the details.<BR>
<BR>
-Brian<BR>
<BR>
<BR>
On 1/3/08 5:46 AM, "Sebastian Stang" <sebastian@yuca-lab.de> wrote:<BR>
<BR>
> Dear pythoneers,<BR>
> i want to deliver my program, which runs perfectly 'uncompiled', as an<BR>
> exe file to customers.<BR>
><BR>
> The problem: i have to use an activeX component in a windows-form, and<BR>
> com-components can only be created in single-thread apartments.<BR>
> But when i use py2exe to compile it, the application is run in a multi-<BR>
> threaded-apartment<BR>
> (System.Threading.Thread.CurrentThread.GetApartmentState() is MTA).<BR>
><BR>
> I tried some workarounds, e.g. create the form in an STA-thread or<BR>
> create the activex in an STA like this:<BR>
><BR>
> thread_is_ready = False<BR>
> axBrowser = None<BR>
> def create_browser_thread(*args):<BR>
> global thread_is_ready<BR>
> global axBrowser<BR>
> print System.Threading.Thread.CurrentThread.GetApartmentState()<BR>
> axBrowser = AxSHDocVw.AxWebBrowser()<BR>
> thread_is_ready = True<BR>
><BR>
> #and the initialization function in my main form class:<BR>
> def InitializeComponent(self):<BR>
> global thread_is_ready<BR>
> global axBrowser<BR>
> job = System.Threading.ThreadStart(create_browser_thread)<BR>
> thread = System.Threading.Thread(job)<BR>
> thread.SetApartmentState(System.Threading.ApartmentState.STA)<BR>
> thread.Start()<BR>
> while not thread_is_ready:<BR>
> pass<BR>
> self.Controls.Add(axBrowser)<BR>
><BR>
> This all worked fine when run with python.exe from command-line.<BR>
> However, compiled with py2exe, the initialization function ends<BR>
> without exceptions, but the form is not shown and the program hangs.<BR>
> Telling the form to shop does not help either.<BR>
><BR>
> Did anyone experience similar problems?<BR>
><BR>
> best regards,<BR>
> Pablo<BR>
> _________________________________________________<BR>
> Python.NET mailing list - PythonDotNet@python.org<BR>
> <a href="http://mail.python.org/mailman/listinfo/pythondotnet">http://mail.python.org/mailman/listinfo/pythondotnet</a><BR>
<BR>
--------------------------<BR>
Brian Lloyd<BR>
<BR>
brian.lloyd@revolutionhealth.com<BR>
<BR>
_________________________________________________<BR>
Python.NET mailing list - PythonDotNet@python.org<BR>
<a href="http://mail.python.org/mailman/listinfo/pythondotnet">http://mail.python.org/mailman/listinfo/pythondotnet</a><BR>
<BR>
</SPAN></FONT></BLOCKQUOTE><FONT FACE="Verdana, Helvetica, Arial"><SPAN STYLE='font-size:12.0px'><BR>
<BR>
</SPAN></FONT></BLOCKQUOTE><FONT FACE="Verdana, Helvetica, Arial"><SPAN STYLE='font-size:12.0px'><BR>
<BR>
--------------------------<BR>
Brian Lloyd<BR>
<BR>
brian.lloyd@revolutionhealth.com<BR>
</SPAN></FONT>
</BODY>
</HTML>