2011-06-06 106 views
0

我已经试图在运行中使用IP附带的一些代码,并且下面的问题,我甚至花了很长时间阅读本书!嵌入式IronPython - 调度程序问题

当我使用下面的代码时,出现错误'expect Delegate,got Function'。 FYI我传递到一个WPF textBox中的引用,因此我应该有一个调度员

我已经删除了所有的穿线管读数东西只是离开“测试”的代码我的UI元素:

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) 

任何线索表示赞赏。

AndyF。

回答

1

由于迪诺Viehland

更改我的调度程序代码来调用调度员直接解决这个问题。

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

可惜我不再从我的打印输出statments实时我的“台” - 脚本完成当这一切出现。删除调度程序并将其恢复为实时...

0

DispatcherExtensions提供了一组调度程序静态方法(扩展方法),它们将Action作为参数。

下面的代码示例演示了WPF调度程序的用法。更多信息请点击这里http://msdn.microsoft.com/en-us/library/cc647497.aspx

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))