CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2009
    Posts
    4

    Question Dispatcher Problem

    Hi Chaps

    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 would be appreciated!

    AndyF.

  2. #2
    Join Date
    Jul 2009
    Posts
    4

    Re: Dispatcher Problem

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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured