VB.net multithread on show/hide/showdialog of another form

WONG Tony 161 Reputation points
2021-07-09T02:55:41.737+00:00

i wish to show another form under multithread
When using show, the label text on another form cannot be shown up
When using showdialog, the label text can be shown but the form cannot be hide after.

How can i fix it ? it seems more complicated in multithread. Thanks.

Private Sub clienttest_Load(sender As Object, e As EventArgs) Handles Me.Load
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False
Try
clientsocket.Connect("192.168.1.3", 8801)
For i = 1 To 3
Dim t As Thread = New Thread(AddressOf Receive)
t.Start(i)
Next
Catch ex As Exception
Application.Exit()
End Try
End Sub

Sub Receive(a)
    While (True)
        If clientsocket.Connected = True Then
            Dim networkstream As NetworkStream = clientsocket.GetStream()
            Dim byesFrom(25) As Byte
            networkstream.Read(byesFrom, 0, byesFrom.Length)
            Dim ServerSignal As String = System.Text.Encoding.ASCII.GetString(byesFrom)

            Select Case Microsoft.VisualBasic.Left(ServerSignal, 1)
                Case "S"   ' show Dialog
                    Me.Hide()
                    test2.Show()
                    'test2.ShowDialog()
                Case "E"  ' Resume from Dialog
                    Me.Show()
                    Me.TopMost = True
                    test2.Hide()
            End Select
        End If
    End While
End Sub
Developer technologies VB
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2021-07-09T03:17:08.153+00:00

    Try something like this:

    System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = True
    . . .
    
    Sub Receive(a)
       While (True)
          If clientsocket.Connected = True Then
             . . .
             Select Case Microsoft.VisualBasic.Left(ServerSignal, 1)
                Case "S"   ' show Dialog
                   Invoke(Sub()
                         Me.Hide()
                         test2.Show()
                      End Sub )
                Case "E"  ' Resume from Dialog
                   Invoke(Sub()
                         Me.Show()
                         Me.TopMost = True
                         test2.Hide()
                      End Sub )
                 End Select
          Else
             Thread.Sle ep(300)
          End If
       End While
    End Sub
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. WONG Tony 161 Reputation points
    2021-07-09T03:08:31.023+00:00

    it is also failed by dispose another form

    another form can be shown up according to the number of thread.

    0 comments No comments

  2. WONG Tony 161 Reputation points
    2021-07-09T03:22:47.24+00:00

    Yes, it show different thread control failed

    but it need a thread to monitor signal from another computer

    any advice? use backgroundworker?

    Thanks a lot

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.