Salve, ho un problema con un new thread e l'apertura di form in mdi (forse non ho ben capito come
controllare i thread).
Il progetto si avvia tramite una form login che apre in hide un form Mdi e poi attiva tramite un
nuovo thread un collegamento socket con un server remoto dal quale ottiene risposta e rende
show la form Mdi.
Dal Server remoto a cadenza di ogni secondo la form Mdi ottiene data e ora del server remoto
e tramite un Delegate la scrive in una label della form mdi.
Fin quì tutto ok.
Ora la form Mdi ha una toolstrip con una serie di Btn che aprono una delle form figlie che duplica
una form diciamo di base, e quando tento di generarne ad esempio una tramite la pressione
del relativo Btn si genera un errore che dice "Impossibile creare un'istanza del controllo ActiveX
'cf906079-727a-4a8c-b532-a34d875aa99e' perché il thread corrente non è un apartment a thread singolo.",
la riga interessata dall'errore è Dim FBat As New FBat
Premetto che se non creo il nuovo thread per il socket le form figlie si generano in maniera corretta.
Sembra quasi che si sia perso il thread principale.
'nella Form Login
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOnLine.Click
FMdi.Hide()
Call Connessione_ClientSock() 'Connette Socket al Server
End Sub
'nella form mdi
Private Sub Btn_Form(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSBtn1.Click,
TSBtn2.Click, TSBtn3.Click, TSBtn4.Click, TSBtn5.Click, TSBtn6.Click, TSBtn7.Click, TSBtn8.Click,
TSBtn9.Click, TSBtn10.Click
Dim FBat As New FBat 'CREA DUPLICATO FORM
FBat.MdiParent = Me 'RENDE LA FORM FIGLIA
FBat.Show()
End Sub
'nel modulo socket
Private ClientSocket As TcpClient
Private ServerStream As NetworkStream
Private Delegate Sub DelegateAddText(ByVal str As String)
Private DelegateMdiText As New DelegateAddText(AddressOf FMdiText)
Private DelegateDataOraText As New DelegateAddText(AddressOf DataOraText)
Public SockThread As Threading.Thread = New Threading.Thread(AddressOf Ricezione_ClientSock)
Private Sub FMdiText(ByVal Stringa As String) 'Delegate causa SockThread
FMdi.Text = Stringa & " CONNESSO"
End Sub
Private Sub DataOraText(ByVal Stringa As String) 'Delegate causa SockThread
FMdi.LblDataOraSock.Text = Stringa
End Sub
Public Sub Connessione_ClientSock()
Try
ClientSocket = New TcpClient
ClientSocket.Connect(IPServer, PortaServerSock)
Call Ritardo(100)
Call Richiesta_autorizzazione()
Catch ex As Exception
ClientSocket.Close()
MessageBox.Show("errore nella connessione sock con il server")
End Try
End Sub
Private Sub Richiesta_autorizzazione()
Dim Stringa As String
If ClientSocket.Connected Then
Stringa = "COMM11" & ";" & UserSock & ";" & PasswSock & ";" & ReleasePrg & ";"
ServerStream = ClientSocket.GetStream()
Dim outStream As Byte() = _
System.Text.Encoding.ASCII.GetBytes(Stringa)
ServerStream.Write(outStream, 0, outStream.Length)
ServerStream.Flush()
SockThread.Start()
Else
Call Chiude_Sock()
End If
End Sub
Public Sub Chiude_Sock()
Dim Stringa As String
If ClientSocket.Connected Then
Stringa = "COMM18" & ";"
ServerStream = ClientSocket.GetStream()
Dim outStream As Byte() = _
System.Text.Encoding.ASCII.GetBytes(Stringa)
ServerStream.Write(outStream, 0, outStream.Length)
ServerStream.Flush()
End If
ClientSocket.Close()
End Sub
Private Sub Ricezione_ClientSock()
Dim Campo()
Dim Stringa As String = ""
Do While True
If ClientSocket.Connected = True Then
If ClientSocket.Available <> 0 Then
'RICEVE DATO
ServerStream = ClientSocket.GetStream()
Dim buffSize As Integer
Dim inStream(10024) As Byte
buffSize = ClientSocket.ReceiveBufferSize
ServerStream.Read(inStream, 0, buffSize)
Dim Dato As String = System.Text.Encoding.ASCII.GetString(inStream)
'FILTRA DATO
Select Case Dato.Substring(0, 6)
Case "COMM12"
'CONFERMA AUTENTICAZIONE RIUSCITA
Campo = Split(Dato, ";")
FLogin.Visible = False 'Nasconde FLogin
FMdi.Show() 'Apre FMdi
FMdi.Invoke(DelegateMdiText, Campo(1))
Case "COMM31"
'RICEVE DATA E ORA DAL SERVER
Campo = Split(Dato, ";")
FMdi.Invoke(DelegateDataOraText, Campo(1))
End Select
End If
End If
Application.DoEvents()
Loop
End Sub
|