[IronPython] Threading/Delegates

Thane thane at magna-capital.com
Mon Aug 9 22:18:47 CEST 2004


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