Results 1 to 9 of 9

Thread: Threading Not working?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Posts
    193

    Threading Not working?

    Im trying to make a thread that accept and reads incoming message but when i send a message it wont display it

    PS: Is their a better to do this with multi-connection?

    Listener
    Code:
    Imports System.Net.Sockets
    Public Class Form1
        Dim MyListener As New TcpListener("6961")
        Dim MyClient As New System.Net.Sockets.TcpClient
        Dim MyNetStream As System.Net.Sockets.NetworkStream
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            MyListener.Start()
            Dim MainThread As Threading.Thread = New Threading.Thread(AddressOf ReadInfo)
        End Sub
    
        Private Sub ReadInfo()
            MyClient = MyListener.AcceptTcpClient
            If MyClient.Connected = True Then
                Try
                    MyNetStream = MyClient.GetStream
                    Dim BytesFrom(MyClient.ReceiveBufferSize) As Byte
                    MyNetStream.Read(BytesFrom, 0, CInt(MyClient.ReceiveBufferSize))
                    Dim ByteToString As String = System.Text.ASCIIEncoding.ASCII.GetString(BytesFrom)
                    Dim MySelect() As String = ByteToString.Split("|")
                    Select Case MySelect(0)
                        Case "Msg"
                            TextBox1.Text = TextBox1.Text + MySelect(1) + " said : " + MySelect(2)
                    End Select
                Catch ex As Exception
                End Try
            End If
        End Sub
    End Class

    Client:
    Code:
    Public Class Form1
        Dim MyClient As New System.Net.Sockets.TcpClient
        Dim MyNetStream As System.Net.Sockets.NetworkStream
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Try
    
                MyClient.Connect(TextBox1.Text, TextBox2.Text)
    
                Button1.Hide()
                Button2.Show()
    
                TextBox1.ReadOnly = True
                TextBox2.ReadOnly = True
    
                MsgBox("Connected to : " & TextBox1.Text & " on Port : " & TextBox2.Text, MsgBoxStyle.Information, Application.ProductName)
            Catch ex As Exception
                MsgBox("Could not connect to : " & TextBox1.Text & " on Port : " & TextBox2.Text, MsgBoxStyle.Critical, Application.ProductName)
            End Try
        End Sub
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Try
                MyClient.Close()
    
                Button1.Show()
                Button2.Hide()
    
                TextBox1.ReadOnly = False
                TextBox2.ReadOnly = False
    
                MsgBox("Disconnected to : " & TextBox1.Text & " on Port : " & TextBox2.Text, MsgBoxStyle.Information, Application.ProductName)
            Catch ex As Exception
    
            End Try
        End Sub
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
                SendMsg("Msg" + "|" + TextBox3.Text + "|" + TextBox4.Text)
        End Sub
        Private Sub SendMsg(ByVal MsgString As String)
            If MyClient.Connected = True Then
                MyNetStream = MyClient.GetStream
                Dim MsgASCII() As Byte = System.Text.ASCIIEncoding.ASCII.GetBytes(MsgString)
                MyNetStream.Write(MsgASCII, 0, MsgASCII.Length)
            End If
        End Sub
    Enb Class
    Last edited by NoobieO.o; Dec 13th, 2009 at 10:37 PM.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Threading Not working?

    You aren't even getting an error with that? You shouldn't be able to write to a control from a background thread without using invocation, but I would have expected a cross-threading error.
    My usual boring signature: Nothing

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Threading Not working?

    You may like to check out the Asynchronous link in my signature. I basically wraps up what you're trying to do: send text between a client and server on a background thread. It uses asynchronous methods rather than synchronous methods on a background thread. The MessageClient and MessageServer classes also handle making sure controls are accessed on the correct thread for you.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Posts
    193

    Re: Threading Not working?

    Hum well i use a while loop to accept and read msg from the client buttttttt while loops are toooooo much memory.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Threading Not working?

    That's why you should use asynchronous methods. You simply call the method on the UI thread and it goes off on it's merry way, not hogging resources or freezing the UI, and you can get on with whatever else you need to do. When the background task is done a callback is invoked on a secondary thread and you can complete the task. You can then delegate back to the UI thread to update the display if required.

    You should check out that code of mine if you haven't already. You're welcome to just reference my DLL as is if you want, which would require nothing more from you than calling some methods and handling some events. Otherwise you can get some ideas from my code to implement in your own.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Posts
    193

    Re: Threading Not working?

    Quote Originally Posted by jmcilhinney View Post
    That's why you should use asynchronous methods. You simply call the method on the UI thread and it goes off on it's merry way, not hogging resources or freezing the UI, and you can get on with whatever else you need to do. When the background task is done a callback is invoked on a secondary thread and you can complete the task. You can then delegate back to the UI thread to update the display if required.

    You should check out that code of mine if you haven't already. You're welcome to just reference my DLL as is if you want, which would require nothing more from you than calling some methods and handling some events. Otherwise you can get some ideas from my code to implement in your own.
    Is their a really good tutorial on UI and asynchronous methods for me to learn?


    Thanks

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Threading Not working?

    Quote Originally Posted by NoobieO.o View Post
    Is their a really good tutorial on UI and asynchronous methods for me to learn?


    Thanks
    I'm not aware of anything specific. All I can suggest is a search, e.g.

    http://www.google.com.au/search?q=.n...ient=firefox-a
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Posts
    193

    Re: Threading Not working?

    Hum, is Asynchronous better then Synchronous.

    If it is why?

    BTW would this be good to learn from? : http://msdn.microsoft.com/en-us/libr...5f(VS.71).aspx

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Threading Not working?

    Quote Originally Posted by NoobieO.o View Post
    Hum, is Asynchronous better then Synchronous.

    If it is why?
    That's like asking whether running is better than walking. Each is better in situations where it's better. Do you know what the terms "asynchronous" and "synchronous" mean? If not then that would be the first thing to sort out. If you know what each of the terms means then that should give you a pretty good idea of when each is more appropriate.

    Most methods you call are synchronous. A synchronous method blocks until the requested operation completes, while an asynchronous method returns immediately and lets you continue while the requested operation is performed in the background.

    The asynchronous method that is most familiar to a lot of people is BackgroundWorker.RunWorkerAsync. The method returns immediately, allowing the UI thread to continue to respond to user input, while the the work is done in the background.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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