Support Forums

Full Version: How can i add a thread?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Code:
Imports System.IO, System.Net, System.Net.Sockets
Public Class Form1

  Dim Client As TcpClient
  Dim sWriter As StreamWriter


  Sub Lees(ByVal ar As IAsyncResult)
    Try
      xUpdate(New StreamReader(Client.GetStream).ReadLine)
      Client.GetStream.BeginRead(New Byte() {0}, 0, 0, AddressOf Lees, Nothing)
    Catch
      xUpdate("Connectie verloren met de server.")
      Exit Sub
    End Try
  End Sub

  Private Sub Verzend(ByVal Str As String)
    Try
      sWriter = New StreamWriter(Client.GetStream)
      sWriter.WriteLine(Str)
      sWriter.Flush()
    Catch
      xUpdate("Er is iets mis met de server.")
    End Try
  End Sub

  Delegate Sub _xUpdate(ByVal Str As String)
  Sub xUpdate(ByVal Str As String)
    If InvokeRequired Then
      Invoke(New _xUpdate(AddressOf xUpdate), Str)
    Else
      If Str.Contains("URL") Then
        Process.Start(Deel("U", Str))
      Else
        TextBox1.AppendText(Str & Environment.NewLine)
      End If
    End If
  End Sub
  Public Function Deel(ByVal strLetter As String, ByVal str As String) As String
    Dim plaats As Integer = str.IndexOf(strLetter) + 3
    Dim lengte As Integer = str.Length - plaats
    Dim data As String = str.Substring(plaats, lengte)
    Return data
  End Function

  Private Sub TextBox2_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
    If TextBox2.Text Is Nothing Then
      MessageBox.Show("Eh fluit, ge moet wel iets invullen")
    End If
    If e.KeyCode = Keys.Enter Then
      Dim strNickname As String = TextBox3.Text
      If strNickname = "" Then
        MessageBox.Show("Gelieve een naam in te vullen")
      Else
        Verzend(strNickname & " zegt: " & TextBox2.Text)
        TextBox2.Text = ""
      End If
    
    End If
  End Sub

  Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
    Dim strNickname As String = TextBox3.Text
    Try
      Client = New TcpClient(txtIp.Text, CInt(3818))
      Client.GetStream.BeginRead(New Byte() {0}, 0, 0, New AsyncCallback(AddressOf Lees), Nothing)
      'xUpdate("Je bent de chat binnengekomen")
      Verzend(strNickname & " is de chat binnengekomen.")

    Catch
      xUpdate("Geen verbinding met de server mogelijk.")
    End Try

  End Sub

  'Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
  '    If Me.WindowState = FormWindowState.Minimized Then
  '        Dim flash As New FLASHWINFO
  '        flash.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(flash) '/// size of structure in bytes
  '        flash.hwnd = MyBase.Handle '/// Handle to the window to be flashed
  '        flash.dwFlags = FLASHW_ALL '/// to flash both the caption bar + the tray
  '        flash.uCount = 100 '/// the number of flashes
  '        flash.dwTimeout = 800 '/// speed of flashes in MilliSeconds ( can be left out )
  '        '/// flash the window you have specified the handle for...
  '        FlashWindowEx(flash)
  '    End If
  'End Sub
  'Public Structure FLASHWINFO
  '    Public cbSize As Int32
  '    Public hwnd As IntPtr
  '    Public dwFlags As Int32
  '    Public uCount As Int32
  '    Public dwTimeout As Int32
  'End Structure



  'Private Declare Function FlashWindowEx Lib "user32.dll" (ByRef pfwi As FLASHWINFO) As Int32

  'Private Const FLASHW_CAPTION As Int32 = &H1
  'Private Const FLASHW_TRAY As Int32 = &H2
  'Private Const FLASHW_ALL As Int32 = (FLASHW_CAPTION Or FLASHW_TRAY)
  Private Sub Form1_FormClosing1(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    Dim strNickname As String = TextBox3.Text
    Verzend(strNickname & " heeft de chat verlaten.")
    End
  End Sub


End Class
This is my code for a chat application for school. It works very good this way. But my teacher wants that i add listener thread. So can someone help me to do this?

It has to we a thread with an endless loop that waits for connections.
It's almost the same as the Client,
PHP Code:
Dim Listener as TcpListener 
Used for the Client that is sending messages, allowing it to both Receive and Send to the other Chat Program with a steady runtime.
What milopeach said!