嵌入式 IronPython - 调度程序问题

发布于 2024-11-13 13:40:33 字数 1408 浏览 3 评论 0原文

我曾尝试窃取《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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光与爱终年不遇 2024-11-20 13:40:33

感谢 Dino Viehland

更改我的调度程序代码以直接调用调度程序修复了此问题。

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

不幸的是,我不再从我的打印语句到我的“控制台”获得实时输出 - 它在脚本完成时全部出现。删除调度程序,它会恢复为实时......

Thanks to Dino Viehland

Changing my dispatcher code to call the dispatcher directly fixes this issue.

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

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

定格我的天空 2024-11-20 13:40:33

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

下面的代码示例演示了 WPF 调度程序的用法。更多信息请访问 http://msdn.microsoft.com/en-us/库/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))

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

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))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文