Hi,
I'm trying to make a IRC client with vb.net
The program must be able to connect to multiple servers at the same time , so not sequential. I know a little about threads from Java but I have no idea what I'm doing wrong. I'll describe what I'm doing now:


Class IRCController:
Manages a collection of IRC objects (see below)
Contains ConnectAll() method that calls each Connect() method of the irc objects. The problem is that when a server is down or takes a long time to connect , the program just waits for the connection te be established or for an timeout before moving to the next irc object. I wanted to solve this with threads. This is my connectall() method (simplified)

Public sub connectall()

For Each oIrc In ircCollection

netw = New Irc()

Dim threadstr As New Threading.ThreadStart(AddressOf netw.Connect)


Dim othread As New Threading.Thread(threadstr)

othread.Start()


Next
End Sub



Class IRC :
Creates a new TCPClient class and handles the IRC protocol.
Contains connect method
Contains also the callback function of the beginread method


Public Sub connect()
Try
mClient.Connect(mServer, mPort)
DisplayText("Connected to host" & vbCrLf)
mStatus = "CONNECTED"
mClient.GetStream.BeginRead(marData, 0, 1000000, AddressOf Me.DoRead, Nothing) 'here is where to error must occur

'msgbox("test") when I uncomment this everything works

Registration()
RaiseEvent Connected(Me)

Catch e As System.Net.Sockets.SocketException
Console.WriteLine(e.Message)
DisplayText(e.ErrorCode & " " & e.Message)
mStatus = "DISCONNECTED"
Disconnect()
RaiseEvent Disconnected(Me, e.ErrorCode & " " & e.Message)

End Try

End Sub


Private Sub DoRead(ByVal ar As IAsyncResult)
Dim intCount As Integer
Try

intCount = mClient.GetStream.EndRead(ar)

If intCount < 1 Then
DisplayText("DISCONNECTED : Stream endread failed")
mStatus = "DISCONNECTED"
Disconnect()
RaiseEvent Disconnected(Me, "DISCONNECTED : Stream endread failed")

Exit Sub
End If


BuildString(marData, 0, intCount)

mClient.GetStream.BeginRead(marData, 0, 1000000, AddressOf Me.DoRead, Nothing)


Catch e As IO.IOException

DisplayText("DISCONNECTED " & e.Message & " " & e.HelpLink() & " " & " " & e.Source)
mStatus = "DISCONNECTED"
Disconnect()
RaiseEvent Disconnected(Me, "DISCONNECTED " & e.Message & " " & e.HelpLink() & " " & " " & e.Source)




End Try
End Sub


I get following exception triggered in the callbackfunction DoRead : "Unable to read data from the transport connection"

BUT however , and this is the strange part : when I uncomment the msgbox("test") in the Connect() function it works and no exceptions are fired.
Probably because each thread is paused by the msgbox . I think there's a conflict with the asynchrous beginread() method of a stream. But I have no idea how to solve this problem.

Any help?


Thank

Maxime