Results 1 to 5 of 5

Thread: [RESOLVED] [VS 2010] [Multi-Threading] Invoke textbox to apply message, from another class.

Threaded View

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2013
    Location
    Norway; Haugesund
    Posts
    39

    Resolved [RESOLVED] [VS 2010] [Multi-Threading] Invoke textbox to apply message, from another class.

    [Visual Studio 2010]

    Hello again!

    I'm doing a TCP server project which works great. However, the chat output won't display in my textbox in the main form. I've analyzed the code a little, but I don't seem to find a workaround. Here's the following code I'm using.

    [NOTE] The TCP server runs on a different thread than the UI main thread.

    Code:
    Public Class cmd
        Public Class doCode
            Public Shared Sub Chat(ByVal [Message] As String)
                Functions.OutputString = [Message]
                Functions.EditTextDocument()
            End Sub
        End Class
    
        Public Class Functions
            Public Shared OutputString As String
            Private Delegate Sub DoStuffDelegate()
            Public Shared Sub EditTextDocument()
    
                If (Form1.InvokeRequired) Then 'Checks if the form needs to be invoked.
                    Form1.Invoke(New DoStuffDelegate(AddressOf EditTextDocument)) 'Invokes the form
                Else
                    Form1.txt_Output.Text &= Environment.NewLine & OutputString 'Adds the output stream from the Chat
                End If
            End Sub
    
        End Class
    
    End Class
    When a message from my TCP server is received, it will call the cmd.doCode.Chat(MessageReceived) sub. The function cmd.doCode.Chat will then try to invoke the textbox and add the message for server monitor display.

    What I've found out so far:

    *When I'm using this code in the main class, with all private, the textbox will get updated.
    *When I use it from a different class than the main class, it won't.

    The conclusion I'm drawing from this, is the way I call it Form1.Invoke might create a new thread, instead of using the main UI thread. I then tested with creating some variables to take the Form1 (Me) object on the journey from the beginning, to the end. Still no luck. In another project I fixed this issue by calling a variable at startup,

    Code:
    Dim myForm as Form = Form1
    but it didn't work in this case. I'm not an expert at threading, and might overlooked some important notes with multi-threading. From what I believe so far, if I use multi-threading in another class, objects on invoke won't get updated.

    My question is therefore, how can I update my objects on main UI thread, while doing multi-threading in a different class?

    Thanks for hearing me out, and I hope you can lead me on the right track!

    - Frek
    Last edited by Frekvens1; Feb 2nd, 2015 at 05:48 AM. Reason: More Info

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