<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, &quot;Maksim Kozyarchuk&quot; &lt;mkozyarchuk@funddevelopmentservices.com&gt; 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, &quot;Brian Lloyd&quot; &lt;brian.lloyd@revolutionhealth.com&gt; 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, &quot;Sebastian Stang&quot; &lt;sebastian@yuca-lab.de&gt; wrote:<BR>
<BR>
&gt; Dear pythoneers,<BR>
&gt; i want to deliver my program, which runs perfectly 'uncompiled', as an<BR>
&gt; exe file to customers.<BR>
&gt;<BR>
&gt; The problem: i have to use an activeX component in a windows-form, and<BR>
&gt; com-components can only be created in single-thread apartments.<BR>
&gt; But when i use py2exe to compile it, the application is run in a multi-<BR>
&gt; threaded-apartment<BR>
&gt; (System.Threading.Thread.CurrentThread.GetApartmentState() is MTA).<BR>
&gt;<BR>
&gt; I tried some workarounds, e.g. create the form in an STA-thread or<BR>
&gt; create the activex in an STA like this:<BR>
&gt;<BR>
&gt; thread_is_ready = False<BR>
&gt; axBrowser = None<BR>
&gt; def create_browser_thread(*args):<BR>
&gt; global thread_is_ready<BR>
&gt; global axBrowser<BR>
&gt; print System.Threading.Thread.CurrentThread.GetApartmentState()<BR>
&gt; axBrowser = AxSHDocVw.AxWebBrowser()<BR>
&gt; thread_is_ready = True<BR>
&gt;<BR>
&gt; #and the initialization function in my main form class:<BR>
&gt; def InitializeComponent(self):<BR>
&gt; global thread_is_ready<BR>
&gt; global axBrowser<BR>
&gt; job = System.Threading.ThreadStart(create_browser_thread)<BR>
&gt; thread = System.Threading.Thread(job)<BR>
&gt; thread.SetApartmentState(System.Threading.ApartmentState.STA)<BR>
&gt; thread.Start()<BR>
&gt; while not thread_is_ready:<BR>
&gt; pass<BR>
&gt; self.Controls.Add(axBrowser)<BR>
&gt;<BR>
&gt; This all worked fine when run with python.exe from command-line.<BR>
&gt; However, compiled with py2exe, the initialization function ends<BR>
&gt; without exceptions, but the form is not shown and the program hangs.<BR>
&gt; Telling the form to shop does not help either.<BR>
&gt;<BR>
&gt; Did anyone experience similar problems?<BR>
&gt;<BR>
&gt; best regards,<BR>
&gt; Pablo<BR>
&gt; _________________________________________________<BR>
&gt; Python.NET mailing list - PythonDotNet@python.org<BR>
&gt; <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>