[IronPython] Threading/Delegates

Dan Lash danlash at gmail.com
Mon Aug 9 23:16:38 CEST 2004


Thanks for the advice about using the VB example. I couldn't find how
to get the address of an object in python (as the VB example says to
do) so I tried just passing in the method, and it works.

And I guess I must have messed up my python class declaration, because
I now can get that working too. Heres the code that works:

from System import *
from System.Threading import *

class ThreadTest:
    def ThreadProc(self):
        for i in range(10):
            Console.WriteLine("ThreadProc: {0}", i)
            Thread.Sleep(500)

    def __init__(self):
        Console.WriteLine("Main thread: Start a second thread.", 1)
        t = Thread(self.ThreadProc)
        t.Start()

        for i in range(4):
            Console.WriteLine("Main thread: Do some work.", 1)
            Thread.Sleep(500)

        Console.WriteLine("Main thread: Call Join(), to wait until
ThreadProc ends.", 1)
        t.Join()
        Console.WriteLine("Main thread: ThreadProc.Join has returned. 
Press Enter to end program.", 1)
        Console.ReadLine()

tt = ThreadTest()


An interesting side note, is if the ThreadProc accepts another
argument, lets say "name", and you try to pass that in when you are
creating the thread, it actually calls the method right then and
there...Oh well.

Thanks Thane (c:


-Dan


On Mon, 9 Aug 2004 13:18:47 -0700, Thane <thane at magna-capital.com> wrote:
> Dan, I think that IronPython acts more like VB than C#, so be sure to read
> the VB comments in MSDN code for ideas.  (Since VB doesn't require a
> ThreadStart object, I thought IronPython wouldn't either.)  Below is what I
> think you were trying to do.  Note that I put the main print loop in one
> line so I could copy/paste the command easily.
> 
> --Thane
> 
> >>> from System import *
> >>> from System.Threading import *
> >>> def foo():
> ....     for i in range(10):
> ....             print i
> ....             Thread.Sleep(2000)      # allow some time to type...
> ....
> >>> t = Thread(foo)
> >>> t
> <System.Threading.Thread object at 0x00000018>
> >>>
> ()
> >>> t.Start()
> >>> 0
> 
> ()
> >>> for i in range(20, 30):     print i;        Thread.Sleep(1000)
> 20
> 1
> 21
> 22
> 2
> 23
> 24
> 3
> 25
> 26
> 4
> 27
> 28
> 5
> 29
> >>> 6
> 7
> 8
> 9
> 
> ()
> 
> 
> >>>
> 
> -----Original Message-----
> From: users-ironpython.com-bounces at lists.ironpython.com
> [mailto:users-ironpython.com-bounces at lists.ironpython.com] On Behalf Of Dan
> Lash
> Sent: Monday, August 09, 2004 12:15 PM
> To: users-ironpython.com at lists.ironpython.com
> Subject: [IronPython] Threading/Delegates
> 
> I've just been playing around with IronPython and some of the .NET
> framework the past few days, and I tried porting over an example off
> of the MSDN library about threading.
> 
> The first thing I noticed is that I can't seem to get any class
> declarations going. Even this doesn't work:
> 
> class Test:
>   def Hello():
>       print "Hello"
> 
> IronPython gives:
> 
> System.Reflection.TargetException: Non-static field requires a target.
>  at System.Reflection.RuntimeFieldInfo.InternalGetValue(Object obj, Boolean
> re
> quiresAccessCheck)
>  at System.Reflection.RuntimeFieldInfo.GetValue(Object obj)
>  at IronPython.Objects.module.__getattribute__(String name)
>  at IronPython.Objects.ModuleDictionary.get_Item(Object key)
>  at IronPython.Objects.Frame.GetGlobal(String name)
>  at input_0.Run(Frame frame)
>  at IronPythonConsole.IronPython.DoInteractive()
> 
> But I guess that may be another issue, what I wanted to get going was
> the following:
> 
> from System import *
> from System.Threading import *
> 
> def ThreadProc():
>   for i in range(10):
>       Console.WriteLine("ThreadProc: {0}", i)
>       Thread.Sleep(0)
> 
> def Main():
>   Console.WriteLine("Main thread: Start a second thread.")
>   t = Thread(ThreadStart(ThreadProc))
>   t.Start()
>   for i in range(4):
>       Console.WriteLine("Main thread: Do some work.")
>       Thread.Sleep(0)
>   Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc
> ends.")
>   t.Join()
>   Console.WriteLine("Main thread: ThreadProc.Join has returned.
> Press Enter to end program.")
>   Console.ReadLine()
> 
> Which is a port (minus the class) out of:
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
> frlrfsystemthreadingthreadclasstopic.asp
> 
> Upon further inspection, it doesn't seem that ThreadStart works on any
> level:
> 
> System.Exception: bad args to this method <constructor# for
> System.Threading.ThreadStart>
>  at IronPython.Objects.ReflectedMethodBase.Call(Object[] args)
>  at IronPython.Objects.ReflectedType.Call(Object[] args)
>  at IronPython.Objects.Ops.Call(Object func, Object arg0)
>  at input_3.Main$f1()
>  at IronPython.Objects.Function0.Call()
>  at IronPython.Objects.Ops.Call(Object func)
>  at input_5.Run(Frame frame)
>  at IronPythonConsole.IronPython.DoInteractive()
> 
> Am I missing something here, is my Python syntax wrong? Or is
> IronPython just not ready for ThreadStart delegates?
> _______________________________________________
> users-ironpython.com mailing list
> users-ironpython.com at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
> 
>



More information about the Ironpython-users mailing list