Quantcast
Channel: Visual Basic Tips and Tricks
Viewing all articles
Browse latest Browse all 2212

Multi trheading utilizzando tcpclient e networkstream

$
0
0
Salve a tutti sono un nuovo utente del forum, ma conosco il sito da diverso tempo, scrivo per la prima volta in questo forum e vorrei scusarmi in anticipo per eventuali errori.
Io ho realizzato un programma in VB.net, premetto che non sono un programmatore ma sono riuscito ad ottenere ci� che voglio, solo che ho qualche problema, mi spiego.
Il programma che ho realizzato � un semplice programma che invia dei comandi a un altro programma che forse qualcuno di voi conoscer�, watchout un programma per gestire display.
Il programma � strutturato in questo modo � composto da 64 bottoni ai quali � possibile assegnare un comando, il funzionamento � molto semplice ogni bottone ha la sua rispettiva textbox nella quale verra inserito il comando da eseguire, e fino a qui tutto funge alla perfezione. I problemi sono sorti da quando ho voluto implemetare un monitor per visualizzare lo stato delle Timeline, per ricevere lo stato della timeline dal watchout ho usato NetworkStreamer, il quale riceve i messaggi di stato in una Richtextbox, e siccome il messaggio che riceve � composto da tante parti che a me non interessano, ho usato la funzione text split per creare una substringa che viene visualizzata in un altra Richtextbox, (siccome ho creato 12 monitor per vedere lo stato di 12 timeline) all'interno della quale ho applicato un controllo If, cio� se la stringa che viene visualizzata nella seconda Richtextbox contiene il testo presente nella textbox usata per assegnare il nome della Timeline che deve monitorare, allora me lo mette in un altro Textbox la quale splitta di nuovo il testo e crea una nuova substringa che mi fa visualizzare lo stato della timeline, per fare cio ho creato un controllo if in quanto la stringa ricevuta � del tipo 0 ... .... ...., a me interessa il numero iniziale della stringa che corrsiponde allo stato della timeline. Tutti questi comandi sono ripetuti per dodici volte e per ognuno ho creato le rispettive TextBox di split, le due RichTextBox sono in comune per tutti i monitor.
Quando avvio i monitor il programma va bene e i monitor rispondono bene, ma dopo che passano qualche minuto il programma diventa inusabile, i bottoni nn rispondo pi� con la latenza che avevano prima cio� 0 quasi nulla, per inviare un comando ci vogliono minuti se riesce ad inviarlo e delle volte non invia niente, io ho creato un Thread per NetworkStreamer. Vorrei riuscire a capire il perch� il programma una volta che avvio il monitor il programma mi diventa instabile. Il monitor invia costantemente dei messaggi, e resta in ascolto sulla stessa porta che il programma usa per inviare i comandi. Se qualcuno da questo romanzo che ho scritto � riuscito a capire il senso del programma e riuscirebbe ad indicarmi su cosa dovr� approfondire la mia conoscenza per poter rendere funzionale in tutto questo programma, io ho pensato che magari i problemi sono causati da problemi di memoria, o magari dovrei poter metter in pausa il monitor, spero che qualcuno riesca a fornirmi qualche consiglio per poter trovare una soluzione.
Allego il codice che ho usato per creare il programma, � composto da una classe che ho chiamato TCPControl che si occupa della connessione e dell'invio dei messaggi, e poi nel mainform ho richiamato la classe NetworkStreamer.
codice:
Public Class TCPControl

     Public Client As TcpClient
     Public DataStream As StreamWriter

   Public Sub New(Host As String, Port As Integer)
      ' CLIENT
       Client = New TcpClient(Host, Port)
       DataStream = New StreamWriter(Client.GetStream)
     End Sub
     Public Sub Send(Data As String)
       DataStream.Write(Data & vbCrLf)
       DataStream.Flush()
     End Sub




End Class
Invece il main si compone cosi
codice:
Imports System.ComponentModel
Imports System.IO
Imports System.Net.Sockets
Imports System.Text

Public Class Form1
   Public Client As TCPControl
   Private Sub Form1_Load(sender As Object, e As EventArgs)Handles MyBase.Load

   End Sub

   Private Sub Button1_Click(sender As Object, e AsEventArgs) Handles Button1.Click
     Client = New TCPControl(ip.Text, port.Text)
     If Client.Client.Connected = True Then
       TextBox1.Text = "Connesso"
       CheckForIllegalCrossThreadCalls = False


       Threading.ThreadPool.QueueUserWorkItem(AddressOfReceiveMessages)
     End If
   End Sub
   Private Sub SendMessage()
     If Client.Client.Connected = True ThenClient.Send(btn1.Text)
   End Sub
   Private Sub SendMessage2()
     If Client.Client.Connected = True ThenClient.Send(btn2.Text)
   End Sub

   Private Sub Button2_Click(sender As Object, e AsEventArgs) Handles Button2.Click
     send.Text = "run"&""& cmd.Text
   End Sub

   Private Sub Button5_Click(sender As Object, e AsEventArgs) Handles Button5.Click
     send.Text = "gotoControlCue"&""& cue.Text &"false"&""& cmd.Text &""&"false"
   End Sub

   Private Sub RadioButton1_CheckedChanged(sender AsObject, e As EventArgs) Handles RadioButton1.CheckedChanged
     If RadioButton1.Checked = True Then
       btn1.Text = send.Text
       send.Text = ""
       cmd.Text = ""
       cue.Text = ""

     End If
   End Sub

   Private Sub RadioButton2_CheckedChanged(sender AsObject, e As EventArgs) Handles RadioButton2.CheckedChanged
     If RadioButton2.Checked = True Then
       btn2.Text = send.Text
       send.Text = ""
       cmd.Text = ""
       cue.Text = ""
     End If
   End Sub

   Private Sub Button3_Click(sender As Object, e AsEventArgs) Handles Button3.Click
     sendmessage3()
   End Sub
   Private Sub sendmessage3()
     If Client.Client.Connected = True ThenClient.Send(sndsts.Text)
   End Sub
   Private Sub Button4_Click(sender As Object, e AsEventArgs) Handles Button4.Click
     SendMessage2()
   End Sub

   Private Sub Button6_Click(sender As Object, e AsEventArgs) Handles Button6.Click
     sendmessage3()
   End Sub
   Private Sub ReceiveMessages(state As Object)
     Try
       While True

         Dim ns As NetworkStream =Client.Client.GetStream()

         Dim toReceive(100000) As Byte
         ns.Read(toReceive, 0, toReceive.Length)
         Dim txt As String =Encoding.ASCII.GetString(toReceive)
         If txtstatus.TextLength > 0 Then
           txtstatus.Text &= vbNewLine & txt &vbLf
           txtstatus.SelectionStart = 0
          ' ns.Write =
         Else
           txtstatus.Text = txt
         End If
       End While
     Catch ex As Exception
       MsgBox(ex.Message)
     End Try
   End Sub

   Private Sub txtstatus_TextChanged(sender As Object, e As EventArgs) Handles txtstatus.TextChanged
     Dim s As String = txtstatus.Text
     Dim words As String() = s.Split(New Char() {":"c})
     Dim word As String
     For Each word In words
       container.Text = word
     Next

   End Sub

   Private Sub statxt_TextChanged(sender As Object, e As EventArgs) Handles statxt.TextChanged
     Dim s As String = statxt.Text
     Dim words As String() = s.Split(New Char() {""c})
     Dim word As String
     For Each word In words
       status1.Text = word
     Next
   End Sub

   Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles status1.TextChanged
     status1.Text = status1.Text.TrimStart
   End Sub

   Private Sub container_TextChanged(sender As Object, e As EventArgs) Handles container.TextChanged
     If container.Text.Contains(txtsts.Text) Then
       statxt.Text = container.Text
       Label6.Text = txtsts.Text


       Dim s1 As String = statxt.Text
       Dim words1 As String() = s1.Split(New Char() {""""c})
       Dim word1 As String
       For Each word1 In words1
         status1.Text = word1
       Next
     End If
     If container.Text.Contains(txtsts2.Text) Then
       Label7.Text = txtsts2.Text
     End If
     Dim s As String = statxt.Text
     Dim words As String() = s.Split(New Char() {""""c})
     Dim word As String
     For Each word In words
       status1.Text = word
     Next

   End Sub

   Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles timeline.TextChanged

   End Sub

   Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
     If RadioButton3.Checked = True Then
       txtsts.Text = timeline.Text
       sndsts.Text = "getStatus 1"&""&""""&"TaskList:mItemList:mItems:TimelineTask \"&""""& txtsts.Text &""""&"\"&""""
       sendmessage3()
       timeline.Text = ""
       sndsts.Text = ""
     End If
   End Sub

   Private Sub RadioButton4_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton4.CheckedChanged
     If RadioButton4.Checked = True Then
       txtsts2.Text = timeline.Text
       sndsts.Text = "getStatus 1"&""&""""&"TaskList:mItemList:mItems:TimelineTask \"&""""& txtsts2.Text &""""&"\"&""""
       sendmessage3()
       timeline.Text = ""
       sndsts.Text = ""
     End If
   End Sub
End Class

Viewing all articles
Browse latest Browse all 2212

Trending Articles


Vimeo 10.7.1 by Vimeo.com, Inc.


UPDATE SC IDOL: TWO BECOME ONE


KASAMBAHAY BILL IN THE HOUSE


Girasoles para colorear


Presence Quotes – Positive Quotes


EASY COME, EASY GO


Love with Heart Breaking Quotes


Re:Mutton Pies (lleechef)


Ka longiing longsem kaba skhem bad kaba khlain ka pynlong kein ia ka...


Vimeo 10.7.0 by Vimeo.com, Inc.


FORECLOSURE OF REAL ESTATE MORTGAGE


FORTUITOUS EVENT


Pokemon para colorear


Sapos para colorear


Smile Quotes


Letting Go Quotes


Love Song lyrics that marks your Heart


RE: Mutton Pies (frankie241)


Hato lada ym dei namar ka jingpyrshah jong U JJM Nichols Roy (Bah Joy) ngin...


Long Distance Relationship Tagalog Love Quotes