I'm trying to understand threading, but I can't find a simple answer as to how it works.
I've been looking at examples, but I am having trouble adapting those examples to what I need, everything relates to objects that are already on the main form. But, mine are not, they are created on event. I don't understand why it is saying anything about forms, as I am experimenting to try and get this to work in any way, so I just have everything sitting on the main form rather than separating it into classes.
Is anyone able to explain what happens when using threading in lamens terms...
I've got a background working thread, I've got a delegate declared, (I think) it needs to hold the data to be passed to the main thread (Form thread)
Within sub TCP, I have a variable = show which is linked to the updateform() delegate. updateform() holds datastring which is passed by the sender.
at the end of the sub tcp - show = addressof txtboxcreate which then runs through all that code.
Where am I supposed to put the code to have the object tb, loaded onto form1.flowlayoutpanel? Is there supposed to be something more in the delegate creation?
<pre>Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports Microsoft.VisualBasic
Imports System.Threading.Thread
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Threading
Public Class main
Private tcpthread As System.Threading.Thread
Public Shared datastring As String
Public Shared counttotal As Integer
Private Sub main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim tcpthread As New System.Threading.Thread(AddressOf tcp)
tcpthread.IsBackground = True
tcpthread.Start()
End Sub
Private Delegate Sub updateform(ByVal datastring)
Private Sub updatefrmtb(ByVal tb As TextBox)
Me.FlowLayoutPanel1.Controls.Add(tb)
End Sub
Private Sub tcp()
Dim show As updateform
Dim lf As Decimal = 23
Dim server As TcpListener
Dim datastring As String = Nothing
server = Nothing
Try
' Set the TcpListener on port 13000.
'Dim port As Int32 = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Iain-KVS", "p1", Nothing)
' Dim ipstring As String = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Iain-KVS", "IP", Nothing)
Dim port As Int32 = 13000
Dim localaddr As IPAddress = IPAddress.Parse("127.0.0.1")
server = New TcpListener(localaddr, port)
' Start listening for client requests.
server.Start()
' server.AcceptSocket()
' Buffer for reading data
Dim bytes(1024) As Byte
Dim data As String = Nothing
' Enter the listening loop.
'While True
' MsgBox("Waiting for a connection... ")
' Perform a blocking call to accept requests.
' You could also user server.AcceptSocket() here.
Dim client As TcpClient = server.AcceptTcpClient()
' Get a stream object for reading and writing
Dim stream As NetworkStream = client.GetStream()
Dim i As Int32
' Loop to receive all the data sent by the client.
i = stream.Read(bytes, 0, bytes.Length)
'While (i <> 0)
' Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
'MsgBox(data)
' Process the data sent by the client.
'data = data.ToUpper()
Dim msg As Byte() = System.Text.Encoding.ASCII.GetBytes(data)
' Send back a response.
stream.Write(msg, 0, msg.Length)
' MsgBox("Sent: {0}", data)
' i = stream.Read(bytes, 0, bytes.Length)
datastring = (data)
' End While
' Shutdown and end connection
client.Close()
'End While
Catch e As SocketException
Console.WriteLine("SocketException: {0}", e)
Finally
server.Stop()
End Try
show = AddressOf txtboxcreate
End Sub 'Main
Private Sub txtboxcreate(ByVal sender As System.Object)
Dim count As Integer = 1
Dim tb As New TextBox
Name = ("TxtBox" & CStr(counttotal))
Dim text As String = datastring
With tb
.Name = Name
.Text = Text
.Multiline = True
.Height = 150
.Width = 250
.ReadOnly = True
.ScrollBars = False
.MaxLength = 100
.Tag = 20
.BackColor = Color.Black
.ForeColor = Color.White
' move unused properties under here'
' tb.ScrollBars = ScrollBars.Both
' tb.MaxLength = 100
' tb.Height = 100
' tb.AutoSize = True
' add events to the textbox created
' AddHandler tb.MouseClick, AddressOf Me.TextBox1_MouseClick
' Form1.textboxestime.Add(tb, 0)
End With
count = (count + counttotal)
' this defines where the textboxes appear
updatefrmtb(tb)
End Sub
End Class</pre><pre></pre>