a few things look sketchy:
* with a
while (true)
loop how does the thread ever terminate? Please don't save that via
Thread.Abort()
.
* there's no need to create a new
TcpListener
for every iteration of the loop. There only needs to be one instance created with
Start
called before the loop, and
Stop
needs to be called only once after the loop.
* that whole bit with
foo
, adding the thread to a list and rolling
foo
back over to 0 when it's 999 is entirely shady. If
foo
rolls over then that's an immediate bug right there. The call to
Add
will insert the new thread at index 998,
processor[998].Start
will be called, then
foo
rolls over. The next time through the loop the thread is added at index 999 but since
foo
rolled over
processor[0].Start
is being called again. Fail. All of this also assumes that no one is ever removing threads from the list, which should be happening somewhere once the threads have finished running to ensure that you don't run out of resources eventually. Also, do other threads access
processor
? If so then it needs to be protected with a
lock
statement.
If all else fails you should be able to use the
Unhandled ExceptionHandler[
^] to help find out the root cause of the problem.