Hi. I'm writing a Client/Multi-threaded Server program on Windows
Vista. It worked fine on Windows Vista, but when the server ran on
Windows XP,
I/O operation has been aborted because of either a thread exit or an
application request
exception randomly occurred at the OnReceive method (asynchronous tcp
stream reading). I searched all over the internet and found a post
posted few years ago. He had the same problem as me, and he said it
seemed like a bug of the .NET framework. He said changing normal
Thread to ThreadPool solved the problem but he couldn't explain the
reason. So I tried his explanation and it really solved the problem
(as least till now)
My original code was:
Dim client As TcpClient = Listener.Accept TcpClient()
Dim t As New Thread(New ParameterizedTh readStart(Addre ssOf
OnNewClient))
t.Start(client)
And I rewrote like:
Dim client As TcpClient = Listener.Accept TcpClient()
ThreadPool.Queu eUserWorkItem(N ew WaitCallback(Ad dressOf OnNewClient),
client)
Could you please tell me what was wrong with my original code? And
using ThreadPool really solves the problem? I mean can there be any
other possiblilities of new problem?
Thank you as always.
--------------------------------------------------
PS, other parts of the code are
Private Sub OnNewClient(ByV al client As Object)
Interlocked.Inc rement(LastID)
If LastID = Integer.MaxValu e Then
LastID = Integer.MinValu e
End If
Dim c As TcpClient = CType(client, TcpClient)
Dim handler As New ClientHandler(c , LastID)
Clients.Add(han dler)
AddHandler handler.Message Received, AddressOf OnMessageReceiv ed
AddHandler handler.Connect ionClosed, AddressOf OnConnectionClo sed
End Sub
Public Class ClientHandler
Public Sub New(ByVal client As TcpClient, ByVal id As Integer)
TheClient = client
_ClientID = id
ClientStream = client.GetStrea m()
Dim state As New StateObject()
state.WorkSocke t = client.Client
client.Client.B eginReceive(sta te.RawBuffer, 0,
StateObject.Buf ferSize, SocketFlags.Non e, New AsyncCallback(A ddressOf
OnReceive), state)
End Sub
Vista. It worked fine on Windows Vista, but when the server ran on
Windows XP,
I/O operation has been aborted because of either a thread exit or an
application request
exception randomly occurred at the OnReceive method (asynchronous tcp
stream reading). I searched all over the internet and found a post
posted few years ago. He had the same problem as me, and he said it
seemed like a bug of the .NET framework. He said changing normal
Thread to ThreadPool solved the problem but he couldn't explain the
reason. So I tried his explanation and it really solved the problem
(as least till now)
My original code was:
Dim client As TcpClient = Listener.Accept TcpClient()
Dim t As New Thread(New ParameterizedTh readStart(Addre ssOf
OnNewClient))
t.Start(client)
And I rewrote like:
Dim client As TcpClient = Listener.Accept TcpClient()
ThreadPool.Queu eUserWorkItem(N ew WaitCallback(Ad dressOf OnNewClient),
client)
Could you please tell me what was wrong with my original code? And
using ThreadPool really solves the problem? I mean can there be any
other possiblilities of new problem?
Thank you as always.
--------------------------------------------------
PS, other parts of the code are
Private Sub OnNewClient(ByV al client As Object)
Interlocked.Inc rement(LastID)
If LastID = Integer.MaxValu e Then
LastID = Integer.MinValu e
End If
Dim c As TcpClient = CType(client, TcpClient)
Dim handler As New ClientHandler(c , LastID)
Clients.Add(han dler)
AddHandler handler.Message Received, AddressOf OnMessageReceiv ed
AddHandler handler.Connect ionClosed, AddressOf OnConnectionClo sed
End Sub
Public Class ClientHandler
Public Sub New(ByVal client As TcpClient, ByVal id As Integer)
TheClient = client
_ClientID = id
ClientStream = client.GetStrea m()
Dim state As New StateObject()
state.WorkSocke t = client.Client
client.Client.B eginReceive(sta te.RawBuffer, 0,
StateObject.Buf ferSize, SocketFlags.Non e, New AsyncCallback(A ddressOf
OnReceive), state)
End Sub
Comment