<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=us-ascii">
<META content="MSHTML 6.00.2900.2963" name=GENERATOR></HEAD>
<BODY>
<DIV dir=ltr align=left><FONT face=Arial color=#0000ff size=2><SPAN
class=571503907-17102006>Hello</SPAN></FONT></DIV>
<DIV dir=ltr align=left><FONT face=Arial color=#0000ff size=2><SPAN
class=571503907-17102006></SPAN></FONT> </DIV>
<DIV dir=ltr align=left><FONT face=Arial color=#0000ff size=2><SPAN
class=571503907-17102006>you are using the module variable ie inside the Generic
Function, but you have to use "d" since this is the Python object
</SPAN></FONT></DIV>
<DIV dir=ltr align=left><FONT face=Arial color=#0000ff size=2><SPAN
class=571503907-17102006>which is allowed to access the COM object in the
separate thread.</SPAN></FONT></DIV>
<DIV dir=ltr align=left><FONT face=Arial color=#0000ff size=2><SPAN
class=571503907-17102006></SPAN></FONT> </DIV>
<DIV dir=ltr align=left><FONT face=Arial color=#0000ff size=2><SPAN
class=571503907-17102006> Stefan</SPAN></FONT></DIV><BR>
<BLOCKQUOTE
style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #0000ff 2px solid; MARGIN-RIGHT: 0px">
<DIV class=OutlookMessageHeader lang=de dir=ltr align=left>
<HR tabIndex=-1>
<FONT face=Tahoma size=2><B>From:</B> Tejovathi P
[mailto:tejovathi.p@gmail.com] <BR><B>Sent:</B> Monday, October 16, 2006 9:47
AM<BR><B>To:</B> Stefan Schukat<BR><B>Subject:</B> Re: Thread
termination<BR></FONT><BR></DIV>
<DIV></DIV>
<P>HI all, </P>
<P>I have a problem in accesing COM objects in threads. To be precise, lets
assume that I have a class GenericFunctions which is defined as follows:</P>
<P>import win32com.client, pythoncom, thread
<BR>ie=win32com.client.Dispatch('internetexplorer.application')
<BR>ie.Visible=1 </P>
<P>class GenericFunctions:<BR>
<BR> def
__init__(self):<BR>
print "In Constructor of Generic
Functions"<BR>
<BR> def
MyNavigate(self,dest):<BR>
ie.Navigate(dest) </P>
<P><BR>Now there is another file Main.py which is defined as
follows:</P>
<P>import win32com.client, pythoncom, thread <BR>from GenericFunctions import
*<BR>obj = GenericFunctions()</P>
<P>class Mainclass:<BR> def
__init__(self);<BR>
print "In Constructor of Main class"</P>
<P> def
threadFunction(self,dest):<BR>
pythoncom.CoInitialize()<BR>
d=pythoncom.CoGetInterfaceAndReleaseStream(s,
pythoncom.IID_IDispatch)<BR>
my_ie=win32com.client.Dispatch(d)<BR>
obj.func(dest) # this is
gving an
error.<BR>
pythoncom.CoUninitialize()</P>
<P>if __name__ == "__main__":<BR>
s=pythoncom.CoMarshalInterThreadInterfaceInStream(pythoncom.IID_IDispatch,ie)<BR>
thread.start_new_thread(self.nav, (s,'<A
href="http://www.google.com">www.google.com </A>')</P>
<P>Basically, I want to access object of GenericFunctions class inside
threadFunction(). However I was able to execute my_ie.Navigate("<A
href="http://google.com">google.com</A>"). But that was not I wanted. I am not
knowing where the error is.... <BR>Please let me know the solution ASAP...</P>
<P>Teja.P</P>
<P><BR> </P><BR><BR>
<DIV><SPAN class=gmail_quote>On 10/13/06, <B class=gmail_sendername>Stefan
Schukat</B> <<A href="mailto:SSchukat@dspace.de">SSchukat@dspace.de</A>>
wrote:</SPAN>
<BLOCKQUOTE class=gmail_quote
style="PADDING-LEFT: 1ex; MARGIN: 0px 0px 0px 0.8ex; BORDER-LEFT: #ccc 1px solid">Reading
from your last posts you are using COM objects. Therefore you<BR>should not
"kill" the thread since this <BR>would lead to reference leaks. So there are
only two ways left:<BR><BR>1. Program a specific interpreter and not use
python.exe which<BR>implements an access to PyThreadState_SetAsyncExc<BR>2.
Check in the separate thread at specific times a variable which is <BR>set
in the main thread.<BR><BR>Both need the try finally construct in the
threadfunction to release COM<BR>objects in the right way.<BR>I.e., (taking
the source from Roger):<BR><BR><BR>def ThreadFunction(istream, dest):
<BR> pythoncom.CoInitialize() // Initialize COM
Runtime for this thread<BR>
try:<BR>
d=pythoncom.CoGetInterfaceAndReleaseStream(istream,<BR>pythoncom.IID_IDispatch)<BR>
my_ie=win32com.client.Dispatch
(d)<BR>
my_ie.Navigate(dest)<BR>
finally:<BR>
my_ie = None // Release COM
object<BR>
pythoncom.CoUninitialize() // Release COM Runtime for
this<BR>thread<BR><BR><BR> Stefan
<BR></BLOCKQUOTE></DIV><BR></BLOCKQUOTE></BODY></HTML>