2011-06-06 109 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))