PDA

View Full Version : problema nella gestione thread [vb.net]


manuel 66
01-06-2012, 13:26
Buongiorno a tutti, sto creando un programma per la condivisione di file che si articola in:
-client1 (invia i dati)
-client2 (riceve i dati)

con il client 1 nn ho problemi ed ecco il codice del thread:


Private Sub trasmetto1()

Control.CheckForIllegalCrossThreadCalls = False

Dim buff As Integer = buffer.Text
Dim c As Integer = 0
Dim totinv As Integer = 0
Dim nb As Integer = Math.Floor(peso / buff)

Dim res As Integer = (peso - (nb * buff)) 'controlla se avanza qualche byte dalla vivisione del peso dal buffer
sendinfo(ext & "/" & peso & "/" & buff & "/" & nb & "/" & res)


Thread.Sleep(5000)


For c = 0 To nb - 1
Dim space As New FileStream(path, FileMode.Open, FileAccess.Read)
space.Seek(c * buffer.Text, SeekOrigin.Current)
Dim tmp(buff) As Byte
space.Read(tmp, 0, buff)
Thread.Sleep(Delay.Text)
connection.Write(tmp, 0, buff)
space.Close()
totinv = totinv + buff
Dim perc As Decimal = ((totinv / peso) * 100)
Label4.Text = perc.ToString("0.00") & " %"
barra.Value = totinv
Next

Thread.Sleep(1000)

If res > 0 Then
Dim space1 As New FileStream(path, FileMode.Open, FileAccess.Read)
space1.Seek(nb * buffer.Text, SeekOrigin.Current)
Dim tmp1(res) As Byte
space1.Read(tmp1, 0, res)
Thread.Sleep(1000)
connection.Write(tmp1, 0, res)
space1.Close()
End If



Label4.Text = "0,00 %"
barra.Value = 0
totinv = 0


'inv1.Suspend()
'inv1.Abort()
en = 1 'abilitato
End Sub


il problema che avevo qui e che ho nel client 2 e che la prima volta che uso il thread.. tutto ok il file viene ricevuto/inviato correttamente, ma la 2 volta..
i thread mi saltavano le istruzioni(if,aggiornamento variabili,msgbox)
nel client 1 ho risolto cosi:

prima avevo semplicemente cosi:

dim inv1 as new thread(adressof trasmetto1)

poi ho risolto con:

dim inv1 as thread

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

inv1=new thread (addressof trasmetto1)
inv1.Start()

End Sub



invece questo non funziona nel client2.. perche ??


Private Sub wait()

Dim file(8192) As Byte


Control.CheckForIllegalCrossThreadCalls = False
tcp = listener.AcceptTcpClient
While (1 > 0)

Try

connection = tcp.GetStream
connection.Read(file, 0, buffer) 'steam mod qui
connection.Flush()



If c = 0 Then
Dim txt As String = ASCII.GetString(File)
txt = txt.Trim

ext = txt.Substring(0, txt.IndexOf("/"))
Label5.Text = "ext: " & ext
txt = txt.Replace(ext & "/", "")
peso = txt.Substring(0, txt.IndexOf("/"))
Label6.Text = "Byte: " & peso
txt = txt.Replace(peso & "/", "")
'barra.Maximum = peso
buffer = txt.Substring(0, txt.IndexOf("/"))
Label7.Text = "buff: " & buffer
txt = txt.Replace(buffer & "/", "")
block = txt.Substring(0, txt.IndexOf("/"))
Label8.Text = "part: " & block
txt = txt.Replace(block & "/", "")
res = txt
Label2.Text = "Res: " & res
barra.Maximum = peso



MsgBox("rivato il tutto")



canc(file)

Try
space = New FileStream("ciao" & ext, FileMode.Create, FileAccess.Write)
Catch
MsgBox("Errore, impossibile creare il File", , "Download")
Exit Sub
End Try

Else
Beep()
space.Write(file, 0, buffer)
tot = tot + buffer
barra.Value = tot
Label4.Text = "peso: " & tot
If tot = peso Then
barra.Value = 0
Label1.Text = "0,00 %"
tot = 0
res = 0
c = -1
i = 0
space.Close()
'<<<<<<<<<<<<<
'receiver.Abort()
'>>>>>>>>>>>>>>>
Beep()
End If
Dim perc As Decimal = (tot / peso) * 100
Label1.Text = perc.ToString("0.00") & " %"
Label3.Text = "Pass: " & c
If c = block Then
buffer = res
End If
canc(file)

End If
c = c + 1
Catch
End Try

End While
End Sub

tutte le correzioni e consigli son ben accetti, grazie.