Beispiel #1
0
    def test_identity(self):
        import IronPythonTest
        from System.Threading import ParameterizedThreadStart, Thread, ThreadStart
        global called
        global globalSelf

        def identity(x):
            return x

        r = IronPythonTest.ReturnTypes()
        r.floatEvent += identity

        import System
        self.assertEqual(r.RunFloat(1.4), System.Single(1.4))

        # try parameterized thread
        a = foo()
        t = Thread(ParameterizedThreadStart(foo.bar))
        t.Start(a)
        t.Join()

        self.assertEqual(called, True)
        self.assertEqual(globalSelf, a)

        # try non-parameterized
        a = foo()
        called = False

        t = Thread(ThreadStart(a.bar))
        t.Start()
        t.Join()

        self.assertEqual(called, True)
        self.assertEqual(globalSelf, a)

        # parameterized w/ self
        a = foo()
        called = False

        t = Thread(ParameterizedThreadStart(a.baz))
        t.Start('hello')
        t.Join()

        self.assertEqual(called, True)
        self.assertEqual(globalSelf, a)
        self.assertEqual(globalArg, 'hello')

        # parameterized w/ self & extra arg, should throw
        try:
            pts = ParameterizedThreadStart(foo.baz)
            pts("Hello")
            self.assertUnreachable()
        except TypeError:
            pass
Beispiel #2
0
    def test_handler_get_invoked(self):
        import IronPythonTest
        from System.Threading import ParameterizedThreadStart, ThreadStart
        def myfunc():
            global myfuncCalled
            myfuncCalled = True

        def myotherfunc(arg):
            global myfuncCalled, passedarg
            myfuncCalled = True
            passedarg = arg

        class myclass(object):
            def myfunc(self):
                global myfuncCalled
                myfuncCalled = True
            def myotherfunc(self, arg):
                global myfuncCalled,passedarg
                myfuncCalled = True
                passedarg = arg

        class myoldclass:
            def myfunc(self):
                global myfuncCalled
                myfuncCalled = True
            def myotherfunc(self, arg):
                global myfuncCalled, passedarg
                myfuncCalled = True
                passedarg = arg

        global myfuncCalled, passedarg

        for target in [myfunc, myclass().myfunc, myoldclass().myfunc]:
            myfuncCalled = False
            ThreadStart(target)()
            self.assertEqual(myfuncCalled, True)

        for target in [myotherfunc, myclass().myotherfunc, myoldclass().myotherfunc]:
            myfuncCalled = False
            passedarg = None

            ParameterizedThreadStart(target)(1)
            self.assertEqual(myfuncCalled, True)
            self.assertEqual(passedarg, 1)

        # verify we can call a delegate that's bound to a static method
        IronPythonTest.DelegateTest.Simple()

        # Untyped delegate. System.Windows.Forms.Control.Invoke(Delegate) is an example of such an API
        self.assertRaises(TypeError, IronPythonTest.DelegateTest.InvokeUntypedDelegate, myfunc)

        myfuncCalled = False
        IronPythonTest.DelegateTest.InvokeUntypedDelegate(IronPythonTest.SimpleDelegate(myfunc))
        self.assertEqual(myfuncCalled, True)

        myfuncCalled = False
        passedarg = 0
        IronPythonTest.DelegateTest.InvokeUntypedDelegate(IronPythonTest.SimpleDelegateWithOneArg(myotherfunc), 100)
        self.assertEqual((myfuncCalled, passedarg), (True, 100))
 def run_many(nthreads, ntimes, nbits):
     lst_threads = []
     for i in range(nthreads):
         t = Thread(ParameterizedThreadStart(foo))
         t.Start((ntimes, nbits))
         lst_threads.append(t)
     for t in lst_threads:
         t.Join()
Beispiel #4
0
    def run(cls, xaml=None):
        '''调用 run 函数启动窗口程序

        :param xaml: xaml 文件路径或者 xaml 格式的 xml 字符串
        '''
        thread = Thread(ParameterizedThreadStart(cls._thread))
        thread.SetApartmentState(ApartmentState.STA)
        thread.Start(xaml)
        thread.Join()
Beispiel #5
0
def run_debugger(py_file):
    if Thread.CurrentThread.GetApartmentState() == ApartmentState.STA:
        t = Thread(ParameterizedThreadStart(run_debugger))
        t.SetApartmentState(ApartmentState.MTA)
        t.Start(py_file)
        t.Join()
    else:
        p = IPyDebugProcess()
        p.run(py_file)
Beispiel #6
0
        def Main():
            sync = Sync()
            t = Thread(ParameterizedThreadStart(ThreadProcParm))
            t.Start(sync)
            t.Join()
            self.assertTrue(sync.hit == 1)

            t = Thread(ThreadStart(ThreadProcNoParm))
            t.Start()
            t.Join()
Beispiel #7
0
def open_tkinter_dialog(title, initial_folder):
    out_queue = Queue()
    out_queue.Enqueue(title)
    out_queue.Enqueue(initial_folder)
    start = ParameterizedThreadStart(tkinter_dialog_thread)
    thread = Thread(start)
    thread.SetApartmentState(ApartmentState.STA)
    thread.Start(out_queue)
    thread.Join()
    final_result = out_queue.Dequeue()
    return final_result[0], final_result[1]