I have not tested this on a GUI screen, but...<br>You should be able to assign any class with a .write() method to sys.stdout (and/or sys.stderr) so that normal "print" statements/functions and tracebacks will be sent to that method. I use the trick frequently to get a simple logging facility by assigning a text file -- like:<br>
<code><br>import sys<br>my_log = open('my_log_file.txt','w')<br>sys.stdout = my_log<br>print 'Hello, Log.'<br>sys.stderr = my_log<br>error = 1 / 0<br></code><br>Is that the kind of 'messages' you were thinking about?<br>
--<br>Vernon<br><br><div class="gmail_quote">On Tue, Jun 7, 2011 at 2:00 AM, AndyF. <span dir="ltr"><<a href="mailto:andy_fulstow@hotmail.com">andy_fulstow@hotmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
Thank you Dino<br>
<br>
modifying my Python dispatcher to read<br>
<div class="im"><br>
dispatcher.BeginInvoke(System.Action(lambda *_: function(*args)))<br>
<br>
</div>fixed the problem. Unfortunately for me, utilising the dispatcher has an<br>
unforseen side effect - the 'messages' printed to my console only appear<br>
when the python script exits.<br>
<br>
If I revert the code to bypass all dispatcher code i.e. use the following<br>
<br>
def write(self, string):<br>
self.texbox.AppendText(string)<br>
<br>
then I see the messages arrive in real time.<br>
<br>
I have tested launching my PythonEngine class using a thread with both<br>
ApartmentState.STA and MTA but this has no discernible effect.<br>
<br>
Can you offer any explanation, maybe workaround?<br>
<br>
Thanks<br>
<br>
AndyF.<br>
<div><div></div><div class="h5"><br>
<br>
<br>
Dino Viehland wrote:<br>
><br>
> AndyF wrote:<br>
>> Hi Chaps<br>
>><br>
>> I have attempted to purloin some of the code shipped with IP in Action<br>
>> and,<br>
>> following issues, I have even gone to the lengths of reading the book!<br>
>><br>
>> I am getting the error 'expect Delegate, got Function' when I use the<br>
>> following code. FYI I am passing in a reference to a WPF textBox so I<br>
>> should<br>
>> have a dispatcher on my UI element<br>
>><br>
>> I have removed all of the threading pipe reading stuff just to leave<br>
>> 'test'<br>
>> code:<br>
>><br>
>> import System<br>
>> import System.IO<br>
>> import Avacta.Optim.Server.WebServices<br>
>> import Avacta.Optim.Server.DataModel<br>
>> import sys<br>
>> import clr<br>
>> import time<br>
>><br>
>> from System import Console<br>
>> from System.Threading import Thread, ThreadStart<br>
>><br>
>> def SetDispatcher(ui_element):<br>
>> global dispatcher # needed else "Exception: 'NoneType' object has no<br>
>> attribute 'BeginInvoke'"<br>
>> dispatcher = ui_element.Dispatcher<br>
>><br>
>> def Dispatch(function, *args):<br>
>> dispatcher.BeginInvoke(lambda *_: function(*args))<br>
>><br>
>> def GetDispatchFunction(function):<br>
>> return lambda *args: Dispatch(function, *args)<br>
>><br>
>> class ListOutput:<br>
>> def __init__(self, textbox):<br>
>> self.textbox = textbox<br>
>><br>
>> def write(self, string):<br>
>> Dispatch(self.addText, string) # error: "expect Delegate, got Function"<br>
>> # self.addText(string) # ok works fine w-w/o dispatcher stuff<br>
>><br>
>> def addText(self, string):<br>
>> textbox.AppendText(string)<br>
>><br>
>> if textbox != None:<br>
>> listout = ListOutput(textbox)<br>
>> sys.stdout = listout<br>
>> SetDispatcher(textbox)<br>
>><br>
>> print "Define running"<br>
>> #running = True<br>
>><br>
>> Thread.Sleep(0)<br>
>> time.sleep(2)<br>
>><br>
>> print "Start The Comms Thread..."<br>
>> #comms_t = Thread(ThreadStart(run_comms))<br>
>> #comms_t.Start()<br>
>><br>
>> Thread.Sleep(0)<br>
>> time.sleep(2)<br>
>><br>
>> Any clues would be greatly appreciated!<br>
><br>
> The WPF invocation functions just take a type which is typed to "Delegate"<br>
> instead of a type of a specific delegate. Therefore we don't know what<br>
> type<br>
> of delegate to create for you automatically. Instead you can convert the<br>
> Python function into a delegate by calling the delegate type directly,<br>
> e.g.:<br>
><br>
> dispatcher.BeginInvoke(System.Action(lambda *_: function(*args)))<br>
><br>
> or you can use another delegate type (e.g. System.Action[object]) with the<br>
> appropriate signature you need.<br>
><br>
><br>
> _______________________________________________<br>
> Users mailing list<br>
> <a href="mailto:Users@lists.ironpython.com">Users@lists.ironpython.com</a><br>
> <a href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com" target="_blank">http://lists.ironpython.com/listinfo.cgi/users-ironpython.com</a><br>
><br>
><br>
<br>
</div></div><font color="#888888">--<br>
View this message in context: <a href="http://old.nabble.com/Dispatcher-Problem-tp31782357p31789855.html" target="_blank">http://old.nabble.com/Dispatcher-Problem-tp31782357p31789855.html</a><br>
</font><div class="im">Sent from the IronPython mailing list archive at Nabble.com.<br>
<br>
_______________________________________________<br>
</div><div><div></div><div class="h5">Users mailing list<br>
<a href="mailto:Users@lists.ironpython.com">Users@lists.ironpython.com</a><br>
<a href="http://lists.ironpython.com/listinfo.cgi/users-ironpython.com" target="_blank">http://lists.ironpython.com/listinfo.cgi/users-ironpython.com</a><br>
</div></div></blockquote></div><br>