Closed computski closed 11 months ago
Whenever you create a TcpSession when receiving a connection, pass a reference to the form. Instead of bubbling up the incoming data via event, override the OnReceived method of the TcpSession and perform the UI update logic via a delegate passed to the Invoke
method of the form control.
Thanks. I implemented Invoke on the form, and data from the TCPSession is being passed to a subroutine inside the form class successfully. No thread errors. The routine processes the data and writes to a textbox on the form UI, except that textbox does not display the new data. using the Update method or application.doEvents does not force the textbox to update either. debug.print proves the data is arriving and even that the .text property of the textbox is holding the new data, but it won't redisplay. Am stumped.
Update. This is strange. If I use Invoke as you suggest from the TCP session, it will call my data processing routine and this routine then tries to update the UI but the textbox does not update. If I pause it in the debugger, the line updating the textbox is running on the main thread (the UI thread) as expected yet it does not update the display.
If instead I go back to bubbling the tcpSession data up as an event to my data processor, the dataprocesser runs on a non UI thread as expected, but when it calls a delegate I wrote to update the UI, this DOES work and the display updates as expected. This is my solution, so I will stop there. Thanks for putting me onto delegates.
closing this out, thx
I took your TCP server and client example, but built the TCP server into a Windows Forms app. When the server is started, I assume the TCP session below it is created on yet another thread. I added an event to report incoming data and bubbled this up through the server but the event causes a cross-thread error if I try to use the event+data to update a control in the UI.
The only way I can make it work is to instead buffer the data in the session class and have the UI poll the server every 100mS for new data via a Background worker. I'd prefer to have this event driven rather than polling - is there a way to do this?