VS 2010 Concverting Console program to Form Program-VBForums
Results 1 to 4 of 4

Thread: Concverting Console program to Form Program

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    105

    Concverting Console program to Form Program

    I have a console program listed below that is working perfectly, taking in 2 different sets of data over ethernet and continually updating and displaying the data stream. The problem is, in my main program im using a form based setup and im having a hard time converting the code over to work in the form based environment.

    Console App Code:
    Code:
    Imports System.Net.Sockets
    Imports System.Text
    
    Module Module1
    
        Sub Main()
    
    
            Dim tcpClient As New System.Net.Sockets.TcpClient()
            tcpClient.Connect("10.200.158.31", 14142)
            Dim networkStream As NetworkStream = tcpClient.GetStream()
    
                If networkStream.CanWrite And networkStream.CanRead Then
                    ' Do a simple write.
                    Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there")
                    networkStream.Write(sendBytes, 0, sendBytes.Length)
    
                Do
                    ' Read the NetworkStream into a byte buffer.
                    Dim bytes(13) As Byte
                    networkStream.Read(bytes, 0, CInt(13))
    
                    ' Output the data received from the host to the console.
                    Dim returndata As String = Encoding.ASCII.GetString(bytes)
                    Console.WriteLine((returndata))
                Loop
    
            Else
                If Not networkStream.CanRead Then
                    Console.WriteLine("cannot not write data to this stream")
                    tcpClient.Close()
                Else
                    If Not networkStream.CanWrite Then
                        Console.WriteLine("cannot read data from this stream")
                        tcpClient.Close()
                    End If
                End If
            End If
    
            ' pause so user can view the console output
            Console.ReadLine()
    
    
    
        End Sub
    
    End Module
    Current Form App Code:
    Code:
    Imports System.Net.Sockets
    Imports System.Text
    
    Class TCPCli
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim tcpClient As New System.Net.Sockets.TcpClient()
            tcpClient.Connect("10.200.158.31", 14142)
            Dim networkStream As NetworkStream = tcpClient.GetStream()
    
            If networkStream.CanWrite And networkStream.CanRead Then
    
                ' Do a simple write.
                Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there")
                networkStream.Write(sendBytes, 0, sendBytes.Length)
    
                Dim bytes(20) As Byte
    
                Do
                    ' Read the NetworkStream into a byte buffer.
                    networkStream.Read(bytes, 0, CInt(20))
                    ' Output the data received from the host to the console.
                    Dim returndata As String = Encoding.ASCII.GetString(bytes)
                    Console.WriteLine((returndata))
                    Label1.Text = Convert.ToString(returndata)
    
                Loop
            End If
        End Sub
    End Class
    Im seeing some data printed to the output screen but not all of the data is there (its only printing at less then half the rate of the console, and im missing the second data set alltogether).

    Also im quite new to VB, and having a tough time trying to figure out how to initiate the procedure above without using a button click event, because when i click the button, the window freezes as its running a continuous neverending loop.

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,990

    Re: Concverting Console program to Form Program

    I think there's a few issues. Your sending your UI thread into an infinite loop that makes the application freeze. You should move you TCP listener code to it's own thread, then use a delegate call to send information back to the form. There are multiple expamples of using worker threads on this forum. Also, the Console.WriteLine isn't going to display anything to your user. The label you're using to display information to the user is getting overriden on every loop iteration, where I would think you would want it to append the data.
    That is the very essence of human beings and our very unique capability to perform complex reasoning and actually use our perception to further our understanding of things. We like to solve problems. -Kleinma

    Does your code in post #46 look like my code in #45? No, it doesn't. Therefore, wrong is how it looks. - jmcilhinney

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    105

    Re: Concverting Console program to Form Program

    Quote Originally Posted by wild_bill View Post
    I think there's a few issues. Your sending your UI thread into an infinite loop that makes the application freeze. You should move you TCP listener code to it's own thread, then use a delegate call to send information back to the form. There are multiple expamples of using worker threads on this forum. Also, the Console.WriteLine isn't going to display anything to your user. The label you're using to display information to the user is getting overriden on every loop iteration, where I would think you would want it to append the data.
    Thanks for your reply, and the reason why i dont have the loop in its own seperate procedure is because i havent been able to figure out how do that myself, also i know the label was getting overwritten i was just using it to see if i was getting and data at all. Also also, the console write commands were still writing to the output box of the VB window.

    Anyway i guess im going to have to search around for these "worker threads", and figure out how to get multiple procedures to work properly.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    31,096

    Re: Concverting Console program to Form Program

    Search for BackgroundWorkerComponent. That is the easiest thing to get to where you want to go, I think. You would want to loop on something, though. Probably what you want to do is put a boolean variable at form scope, set it to true before you call the DoWork of the background worker, then have the worker loop while the boolean is true. What this will do is give you a means to end that perpetual loop from outside of the BGW. The UI can set the Boolean to False and the loop will terminate the next time it loops.
    My usual boring signature: Nothing

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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.