I have written an application using VB.Net 2003 which contains a single timer
control (Interval set to 5 seconds) and a function which uses the
BeginReceiveFrom Socket function.
The problem I am having is that everytime the timer runs and calls the
function which sends and receives data through the UDP protocol, it increases
the systems thread count by 1.
After the software running for around 1 hour windows cracks it as I am using
too many threads.
Does anybody know why this is happening and hope I can stop it??
CODE:
Public Sub UDPTest()
' Create endpoint
remoteIpEndPoint = New System.Net.IPEndPoint(System.Net.IÂ*
PAddress.Parse('192.168.10.200'), Â* 100)
' Create a UDP scoket for sending data
udpSocket = New Socket(AddressFamily.InterNetwork,Â* SocketType.Dgram,
ProtocolType.Udp)
' Connect UDP Socket to device
udpSocket.Connect(remoteIpEnÂ* dPoint)
' Check if writeable then send data request to device
If udpSocket.Poll(-1, SelectMode.SelectWrite) Then
udpSocket.Send(sendBytes, sendBytes.Length, SocketFlags.None)
End If
udpSocket.BeginReceiveFrom(bÂ* ytes, 0, bytes.Length, SocketFlags.None,
remoteIpEndPoint, New AsyncCallback(AddressOf OnReceive), Nothing)
Console.Write(Encoding.ASCIIÂ* .GetString(bytes))
End Sub
Public Sub OnReceive(ByVal ar As IAsyncResult)
On Error GoTo ErrorHandle
Dim iBytes As Integer = udpSocket.EndReceiveFrom(ar, remoteIpEndPoint)
ErrorHandle:
End Sub