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
BeginReceiveFro m 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
remoteIpEndPoin t = New System.Net.IPEn dPoint(System.N et.IÂ*
PAddress.Parse( '192.168.10.200 '), Â* 100)
' Create a UDP scoket for sending data
udpSocket = New Socket(AddressF amily.InterNetw ork,Â* SocketType.Dgra m,
ProtocolType.Ud p)
' Connect UDP Socket to device
udpSocket.Conne ct(remoteIpEnÂ* dPoint)
' Check if writeable then send data request to device
If udpSocket.Poll(-1, SelectMode.Sele ctWrite) Then
udpSocket.Send( sendBytes, sendBytes.Lengt h, SocketFlags.Non e)
End If
udpSocket.Begin ReceiveFrom(bÂ* ytes, 0, bytes.Length, SocketFlags.Non e,
remoteIpEndPoin t, New AsyncCallback(A ddressOf OnReceive), Nothing)
Console.Write(E ncoding.ASCIIÂ* .GetString(byte s))
End Sub
Public Sub OnReceive(ByVal ar As IAsyncResult)
On Error GoTo ErrorHandle
Dim iBytes As Integer = udpSocket.EndRe ceiveFrom(ar, remoteIpEndPoin t)
ErrorHandle:
End Sub