Ho creato una chat utilizzando i socket di sistema (tcpClient e tcpListener, contenuti in system.net.sockets) ho creato quindi una dll ke mi fa da interfaccia per questi socket. Il problema per ora me lo da nella parte che fa da server,ma darebbe lo stesso errore nella parte client.Dopo aver avviato il server
e sino a qui tutto a posto,codice:Public Sub startServer() 'metodo ke lancia l'ascolto del server socketListener = New TcpListener(localIp.IpMyIpAddress, port) createThread(New System.Threading.ThreadStart(AddressOf serverListening)) End Sub Private Sub serverListening() Dim maskTcp As New TcpClient Try socketListener.Start() stateConn = True While True maskTcp = socketListener.AcceptTcpClient'chiamata bloccante
dopo aver ricevuto una richiesta di connessione genero un evento che la notifica:
il problema arriva ora. nella form in cui gestisco questa notifica in questo modo:codice:RaiseEvent connectionOpened(maskTcp.GetHashCode)
mi genera questo errore:codice:Private Sub sktServer_connectionOpened(ByVal socketIndex As Object) Handles sktServer.connectionOpened lstLog.Items.Add(Now.ToLongTimeString & " Connesso il client n° " & socketIndex.ToString) End Sub
come posso risolvere questa cosa? a mio parere è un qualcosa sulla sicurezza, perchè lo stesso programma in vb.net non da problemi. ma per necessità devo farlo in vb 2005(per ora express poi acquisterò il professional)Grazie in anticipoSystem.InvalidOperationException was caught
Message="Cross-thread operation not valid: Control 'lstLog' accessed from a thread other than the thread it was created on."
Source="System.Windows.Forms"
StackTrace:
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.SendMessage(Int32 msg, Int32 wparam, String lparam)
at System.Windows.Forms.ListBox.NativeAdd(Object item)
at System.Windows.Forms.ListBox.ObjectCollection.AddI nternal(Object item)
at System.Windows.Forms.ListBox.ObjectCollection.Add( Object item)
at soket.Form1.sktServer_connectionOpened(Object socketIndex) in C:\Documents and Settings\Administrator.GIULIANINET\Documenti\soket \soket\soket\Form1.vb:line 47
at socketLib.socketServer.serverListening() in C:\Documents and Settings\Administrator.GIULIANINET\Documenti\soket \soket\socketLib\socketServer.vb:line 64