Hello,
I'm trying to dive into what I consider a deeper end of VB.net to see how far I can go with it.
I have a project created, ironically called "My First Server".
The aim of this is to create a server which can handle multi clients but additionally isn't the commonly shown tutorial setup of a console application.
So it is created as a Windows Form application.
The main form (Public Class PrimaryForm) consists of only one control which is a listbox control. This is named LogOutput.
The code within the PrimaryForm (at present) is:
As you can see, I have a second class which is called upon called ServerClass.
This is in a seperate file entirely and looks like this.
Upon running the code... The LogOutput is my eyes to show me whats happening at the time (as you'd expect I guess by the name).
The first line of the Load for PrimaryForm works and displays a welcome message.
I also know that the ServerClass is called and the thread started because it's call back to PrimaryForm's MSG function shows the debug message in Visual Studio.
However, it's not updating the ListBox with the entries from ServerClass for example this:
I've done a bit of reading about and I understand there's considered an unsafe and safe method of threads talking to controls created by other threads and believe I've covered that and proven it's working if all held in the same class but when it's two seperate classes it doesn't work.
If anyone can debug the reason and/or provide me advice I'd be greatly appreciative.
Summary of the programmed function above is at first to open a listener and write updates to a listbox log.
Kind regards,
DevLex
I'm trying to dive into what I consider a deeper end of VB.net to see how far I can go with it.
I have a project created, ironically called "My First Server".
The aim of this is to create a server which can handle multi clients but additionally isn't the commonly shown tutorial setup of a console application.
So it is created as a Windows Form application.
The main form (Public Class PrimaryForm) consists of only one control which is a listbox control. This is named LogOutput.
The code within the PrimaryForm (at present) is:
Public Class PrimaryForm
Private Sub PrimaryForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LogOutput.Items.Add("Welcome to the 'Server'. The current date and time is " & Date.Now)
Dim Server As New ServerClass
Server.Start()
End Sub
Public Sub Msg(ByVal mesg As String)
If Me.LogOutput.InvokeRequired Then
Dim d As New MsgCallBack(AddressOf Msg)
Me.Invoke(d, New Object() {mesg})
Else
mesg.Trim()
Me.LogOutput.Items.Add(Date.Now & mesg)
Debug.WriteLine(Date.Now & mesg)
End If
End Sub
Delegate Sub MsgCallBack(ByVal mesg As String)
End Class
As you can see, I have a second class which is called upon called ServerClass.
This is in a seperate file entirely and looks like this.
Imports System.Net.Sockets
Imports System.Net
Imports System.Threading
Public Class ServerClass
Private ServerThread As New Thread(New ThreadStart(AddressOf StartServer))
Public Sub Start()
ServerThread.Start()
End Sub
Private Sub StartServer()
Dim listener As New TcpListener(IPAddress.Any, 8888)
Dim client As TcpClient
Dim counter As Integer = 0
PrimaryForm.Msg(" >> Started Server")
listener.Start()
client = listener.AcceptTcpClient
PrimaryForm.Msg(" >> Client Connected")
End Sub
End Class
Upon running the code... The LogOutput is my eyes to show me whats happening at the time (as you'd expect I guess by the name).
The first line of the Load for PrimaryForm works and displays a welcome message.
I also know that the ServerClass is called and the thread started because it's call back to PrimaryForm's MSG function shows the debug message in Visual Studio.
However, it's not updating the ListBox with the entries from ServerClass for example this:
PrimaryForm.Msg(" >> Started Server")
I've done a bit of reading about and I understand there's considered an unsafe and safe method of threads talking to controls created by other threads and believe I've covered that and proven it's working if all held in the same class but when it's two seperate classes it doesn't work.
If anyone can debug the reason and/or provide me advice I'd be greatly appreciative.
Summary of the programmed function above is at first to open a listener and write updates to a listbox log.
Kind regards,
DevLex