Thread stops after reciving a UDP packet

F

Feudalac!

I am trying to send an UDP packet and recive it.

The problem is that when the packet is recived, the thread stops and i
cant start it again...



here is the code


Public Class Form1

Public UDP_Reciving As System.Net.Sockets.UdpClient
Public IpSource As New
System.Net.IPEndPoint(System.Net.IPAddress.Any, 65433)
Public ThreadReceive As System.Threading.Thread

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim UDP_Sending As New System.Net.Sockets.UdpClient

UDP_Sending.Connect(System.Net.IPAddress.Parse("255.255.255.255"),
65432)

UDP_Sending.Send(System.Text.Encoding.ASCII.GetBytes(txtNewText.Text),
System.Text.Encoding.ASCII.GetBytes(txtNewText.Text).Length)
UDP_Sending.Close()

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

MsgBox(ThreadReceive.IsAlive, MsgBoxStyle.Critical, "m")


-- on first click this box is 'true' but on every other click the box
is 'false'
i have checked with Ethereal - the packets are send successfuly...
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
UDP_Reciving = New System.Net.Sockets.UdpClient(65432)
ThreadReceive = New System.Threading.Thread(AddressOf
ReceiveMessages)
ThreadReceive.Start()
End Sub

Private Sub ReceiveMessages()
Dim receiveBytes As [Byte]() = UDP_Reciving.Receive(IpSource)
Dim BitDet As BitArray
BitDet = New BitArray(receiveBytes)
Dim strReturnData As String =
System.Text.Encoding.Unicode.GetString(receiveBytes)

Dim intI As Integer
Dim strTemp As String = ""
For intI = 0 To receiveBytes.Length - 1
strTemp = strTemp & Chr(receiveBytes(intI).ToString)
Next
SetText(IpSource.Address.ToString & " " & strTemp)
End Sub

Delegate Sub SetTextCallback(ByVal [text] As String)
Private Sub SetText(ByVal [text] As String)
If Me.txtHistory.InvokeRequired Then
Dim d As New SetTextCallback(AddressOfeudalac!
sfgsdfgsdfgf SetText)
Me.Invoke(d, New Object() {[text]})
Else
Me.txtHistory.Text = [text]
End If
End Sub

End Class
 
V

Vadym Stetsyak

Hello, Feudalac!!

F> The problem is that when the packet is recived, the thread stops and i
F> cant start it again...

Where does it stop? On SetText method? Are there any exceptions being thrown?

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
F

feudalac

Vadym said:
Hello, Feudalac!!

F> The problem is that when the packet is recived, the thread stops
and i F> cant start it again...

Where does it stop? On SetText method? Are there any exceptions being
thrown?

no exceptions are being thrown.
the thread stops when it completes execution (right after 'end sub')
i tested that by putting a message box at the very end of the sub
 
F

feudalac

Vadym said:
Hello, feudalac!

Are you somehow interacting with GUI part from within your threads
that handle network I/O?

yes...
when a packet is decoded, it's value is displayed on a form (that works
- thread safe way)
 
W

William Stacey [MVP]

I might use BeginInvoke instead as it does not block the worker thread and
avoid the possibility of a dead lock.

--
William Stacey [MVP]

| Hello, feudalac!
|
| I suspect that you're not working with GUI in proper way...
|
| To work with GUI part from another thread one must marshal the calls to
the GUI thread via Control.Invoke.
|
| Take a look at
| ( http://www.codeproject.com/csharp/winformthreading.asp )
|
| --
| Regards, Vadym Stetsyak
| www: http://vadmyst.blogspot.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top