Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdate UI form from worker thread
    text
    copied!<p>I am new to multi-threading in VB.NET and have come across a problem whereby I am wanting to append text to a text box on a form from a service thread running in the background.</p> <p>The application I am developing is a client/server listener, I have been able to get the client and server PC's to talk with each other (confirmed through MsgBox), however I am now struggling to get the service thread on the server to append the text to the textbox, nothing vissible occurs.</p> <p>I have a form named testDebug which calls a class (RemoteSupport), this class does all the handshake tasks and updates the textbox with the connection data.</p> <p>Can anyone identify where I am going wrong and point me in the right direction?</p> <p>The following is the code I have: The form has a textbox named txtOutput, the following is from the remoteSupport class</p> <pre><code>Dim outMessage As String = (encoder.GetString(message, 0, bytesRead)) MsgBox(outMessage, MsgBoxStyle.Information, "MEssage Received") If outMessage IsNot Nothing Then If testDebug.InvokeRequired Then ' have the UI thread call this method for us testDebug.Invoke(New UpdateUIDelegate(AddressOf HandleClientComm), New Object() {outMessage}) ' Else testDebug.txtOutput.AppendText(outMessage) End If 'RaiseEvent MessageReceived(outMessage) // a previous attempt to use custom events End If </code></pre> <p>I am not sure if the invoke method is the ideal solution or if custom events are, I have spent some time on trying to get custom events to work, but these didnt work either.</p> <pre><code> // In the RemoteSupport class Public Delegate Sub MessageReceivedHandler(ByVal message As String) Public Shared Event MessageReceived As MessageReceivedHandler // Located throughout the RemoteSupport class where debug information is required. RaiseEvent MessageReceived(outMessage) // Located in the code-behind of the form Private Sub Message_Received(ByVal message As String) testDebugOutput(message) // this is a function I have created // to append the text to the text box End Sub </code></pre> <p>The code supplied has been cut down so if there is anything else that you want to see or any questions please let me know.</p> <p>Thanks for your assistance.</p> <p>EDIT: I have uploaded the two VB files (form and class) to my site, I would appreciate it if someone could have a look at it to help me with identifying the problem with the UI not updating.</p> <p>I have tried a few other things but nothing seems to be updating the UI once the worker thread has started.</p> <p>Form: mulholland.it/testDebug.vb.txt Class: mulholland.it/remoteSupport.vb.txt</p> <p>Thanks for your assistance.</p> <p>Matt</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload