Multithreading 嵌入式IronPython调度程序问题

Multithreading 嵌入式IronPython调度程序问题,multithreading,ironpython,dispatcher,Multithreading,Ironpython,Dispatcher,我曾试图盗取IP附带的一些代码,在出现问题之后,我甚至读了这本书 当我使用下面的代码时,我得到了错误“expect Delegate,Get Function”。仅供参考,我正在传递对WPF文本框的引用,因此我的UI元素上应该有一个dispatcher 为了留下“测试”代码,我删除了所有线程管道读取内容: import System import System.IO import Avacta.Optim.Server.WebServices import Avacta.Optim.Server

我曾试图盗取IP附带的一些代码,在出现问题之后,我甚至读了这本书

当我使用下面的代码时,我得到了错误“expect Delegate,Get Function”。仅供参考,我正在传递对WPF文本框的引用,因此我的UI元素上应该有一个dispatcher

为了留下“测试”代码,我删除了所有线程管道读取内容:

import System
import System.IO
import Avacta.Optim.Server.WebServices
import Avacta.Optim.Server.DataModel
import sys
import clr
import time

from System import Console
from System.Threading import Thread, ThreadStart

def SetDispatcher(ui_element):
    global dispatcher # needed else "Exception: 'NoneType' object has no attribute 'BeginInvoke'"
    dispatcher = ui_element.Dispatcher 

def Dispatch(function, *args):
    dispatcher.BeginInvoke(lambda *_: function(*args))

def GetDispatchFunction(function):
    return lambda *args: Dispatch(function, *args)

class ListOutput:
    def __init__(self, textbox):
    self.textbox = textbox

def write(self, string):
    Dispatch(self.addText, string) # error: "expect Delegate, got Function"
    #self.addText(string) # ok works fine w-w/o dispatcher stuff

def addText(self, string):
    textbox.AppendText(string)

if textbox != None:
    listout = ListOutput(textbox)
    sys.stdout = listout
    SetDispatcher(textbox)

print "Define running"
#running = True

Thread.Sleep(0)
time.sleep(2)

print "Start The Comms Thread..."
#comms_t = Thread(ThreadStart(run_comms))
#comms_t.Start()

Thread.Sleep(0)
time.sleep(2)
任何线索都很感激


谢谢迪诺·维兰

更改dispatcher代码以直接调用dispatcher可以解决此问题

dispatcher.BeginInvoke(System.Action(lambda *_: function(*args)))

不幸的是,我不再从打印状态获取实时输出到我的“控制台”-这一切都会在脚本完成时出现。删除dispatcher并将其恢复为实时…

有一组dispatcher静态方法(扩展方法)是通过DispatcherExtensions提供的,DispatcherExtensions将操作作为参数

下面的代码示例演示了WPF调度程序的用法。这里有更多信息


老实说,我不知道这是否是这个问题的好答案,但它告诉我从哪里可以找到调度员,所以我对此投了赞成票。☺
import clr
clr.AddReference('WindowsBase')
clr.AddReference('System.Windows.Presentation')
from System import Action
from System.Windows.Threading import DispatcherExtensions, Dispatcher

dispatcher = Dispatcher.CurrentDispatcher

def workCallBack():
    print 'working'

DispatcherExtensions.BeginInvoke(dispatcher, Action(workCallBack))