嵌入式 IronPython - 调度程序问题
我曾尝试窃取《IP in Action》附带的一些代码,并且在出现问题后,我什至花了大量时间阅读了这本书!
当我使用以下代码时,我收到错误“期望委托,得到函数”。仅供参考,我正在传递对 WPF 文本框的引用,因此我的 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)
任何线索都值得赞赏。
安迪·F.
I have attempted to purloin some of the code shipped with IP in Action and, following issues, I have even gone to the lengths of reading the book!
I am getting the error 'expect Delegate, got Function' when I use the following code. FYI I am passing in a reference to a WPF textBox so I should have a dispatcher on my UI element
I have removed all of the threading pipe reading stuff just to leave 'test' code:
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)
Any clues appreciated.
AndyF.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
感谢 Dino Viehland
更改我的调度程序代码以直接调用调度程序修复了此问题。
不幸的是,我不再从我的打印语句到我的“控制台”获得实时输出 - 它在脚本完成时全部出现。删除调度程序,它会恢复为实时......
Thanks to Dino Viehland
Changing my dispatcher code to call the dispatcher directly fixes this issue.
Unfortunately I no longer get real-time output from my print statments to my 'console' - it all appears when the script completes. Remove the dispatcher and it reverts to real-time...
DispatcherExtensions 提供了一组调度程序静态方法(扩展方法),它以 Action 作为参数。
下面的代码示例演示了 WPF 调度程序的用法。更多信息请访问 http://msdn.microsoft.com/en-us/库/cc647497.aspx
There is a set of dispatcher static methods (extension methods) are provided by way of DispatcherExtensions which take an Action as the parameter.
The code sample below demonstrates the usage of the WPF dispatcher. More information is available here http://msdn.microsoft.com/en-us/library/cc647497.aspx