dcsimg
Results 1 to 7 of 7

Thread: [RESOLVED] Threading issue

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    285

    Resolved [RESOLVED] Threading issue

    Hello.

    I am having problems printing my incoming data in a textbox which I use for data log atm.

    What happens is, I fire up my client and connect to the server. So far, all is well, and my incoming data is being printed on frmClient. However, as soon as I "Enter the world" (which is the actual game and starts up in a new thread), all printing stops, and I get no error message whatsoever. The data is arriving just fine, it's just not being logged.

    I figured this has to do my new thread being forbidden to access the UI thread. I've been messing around with Invoke, but had no success at all.

    At the bottom of my "ReceiveData" sub:

    Code:
    If PrintData = True Then
        Call frmClient.PrintToChatLog(Data(i))
    End if
    In frmClient:

    Code:
    Delegate Sub _PrintToChatLog(str As String)
    
    Public Sub PrintToChatLog(ByVal str As String)
        If Me.InvokeRequired = True Then
             Invoke(New _PrintToChatLog(AddressOf PrintToChatLog), str)
        Else
             txtChatLog.AppendText(str & vbNewLine)
        End if
    End Sub

  2. #2
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Threading issue

    Is frmClient the default instance of the form?

    If so, that is likely the issue as accessing the default instance creates a unique instance for each thread context that accesses it.
    Last edited by TnTinMN; Jan 3rd, 2017 at 07:30 PM.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    285

    Re: Threading issue

    Yes, I believe so. If I understand you correctly, my new thread is creating its own "copy" of frmClient, which is the copy I am not seeing on my screen? That would also explain why I'm not getting any errors, which has been bugging me.

    If so, is there any remedy to this?

  4. #4
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Threading issue

    Pass a reference of the form to the thread when you start it and store it for local consumption.

    If you do not know how to do this, please show the code you are using to to start your game thread.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    285

    Re: Threading issue

    I'm going out for a walk, I'll experiment more when I get back.

    This is the code you asked for:

    Code:
    Dim FF As Thread = New Thread(New ThreadStart(AddressOf frmClient.StartGame))
    FF.Start()
    Code:
    Public Sub StartGame()
    
    'Create a new XNA game instance
            Dim game As New Game
    
    'Run the game
            game.Run()
    
    End Sub

  6. #6
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: Threading issue

    A simplistic approach would be something like this:
    Code:
    Class Game
    	Public frm As frmClient
    	Public Sub Run()
    	End Sub
    	Sub ReceiveData()
    		frm.Invoke(New Action(Of String)(AddressOf frmClient.PrintToChatLog), New Object() {"string to send"})
    	End Sub
    End Class
    Code:
    Public Sub StartGame(param As Object)
    'Create a new XNA game instance
    	Dim game As New Game
    	game.frm = DirectCast(param, frmClient)
    'Run the game
    	game.Run()
    
    End Sub
    Code:
    Dim FF As Thread = New Thread(AddressOf frmClient.StartGame)
    FF.Start(frmClient)
    I really do not like the above method as it requires Game to know about frmClient. This is a stylistic issue and I typically don't care to get into those types of discussions, but instead of the previous code I would probably pass two objects: 1) an instance System.ComponentModel.ISynchronizeInvoke (the Form class implements this interface) and 2) a delegate to the PrintToChatLog method. Then use the ISynchronizeInvoke to execute the delegate, but that may be a bit confusing at this point.

    It may help to read:
    Parallel Computing - It's All About the SynchronizationContext

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2014
    Posts
    285

    Re: Threading issue

    I got it working using your example. Thanks a lot for your help. Unfortunately the forums won't allow me to rate you at this moment in time.

    Your link will provide me with some bedtime reading one of these days I'm sure

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width