472,958 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Thread in VB.NET won't stop (w/code)

I have a windows application that does not stop running whenever the
application exits.

Could someone fill me in on what I am doing wrong?

Here is the relevant code:
=================================
Private m_thTCP As Thread
Private m_listener As TcpListener

Public Sub New()
InitializeComponent() ' This call is required by the Windows Form
Designer.
m_thTCP = New Thread(AddressOf TCPServer)
ThreadStart()
End Sub

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
ThreadStop()
End Sub

Private Sub Restart_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles RestartButton.Click
ThreadRestart()
End Sub

Private Sub ThreadStop() ' Stopping TCP Listener Service
Try
If (m_thTCP.ThreadState <ThreadState.Stopped) Then
Dim i As Int16 = 0
m_listener.Stop()
Application.DoEvents()
Thread.Sleep(200)
While ((i < 100) And (m_thTCP.ThreadState <ThreadState.Stopped))
' gives thread 5 secs to stop!
m_thTCP.Abort()
Application.DoEvents()
Thread.Sleep(50)
i += 1
End While
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
Console.WriteLine("ThreadState is {0}", m_thTCP.ThreadState.ToString())
m_thTCP = Nothing
End Try
End Sub

Public Sub ThreadRestart() ' Restarting TCP Listener Service
Try
ThreadStop()
m_thTCP = New Thread(AddressOf TCPServer)
ThreadStart()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Private Sub ThreadStart()
Try
m_thTCP.Start()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub

Private Sub TCPServer()
Try ' Starting TCP Listener from thread
m_listener = New TcpListener(IPAddress.Any, m_port)
m_listener.Start()
While True
Dim client As TcpClient = m_listener.AcceptTcpClient()
' Waits until data is available on the network
Dim stream As NetworkStream = client.GetStream()
Dim bytes(client.ReceiveBufferSize) As Byte
stream.Read(bytes, 0, CInt(client.ReceiveBufferSize))
Dim data As String = Encoding.ASCII.GetString(bytes)
data = data.TrimEnd(data.Substring(data.Length - 1))
If (data.Substring(3) <String.Empty) Then
Dim item As String = data.Substring(3)
If (item <"") Then
Console.WriteLine("Data Read: {0}", item)
End If
End If
stream.Close()
client.Close()
End While
Catch ex2 As SocketException
Console.WriteLine("SocketException: {0}", ex2)
Catch ex1 As ThreadAbortException ' dismiss this one
Exit Sub
Catch ex As Exception
MsgBox(ex.Message)
Finally
Try
m_listener.Stop()
Catch ex As Exception ' Throws Exception if it never was opened
End Try
End Try
End Sub
Jun 27 '08 #1
0 1271

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Harald Armin Massa | last post by:
I need to do some synchronisations like in a cron.job import time from threading import Thread class updater(Thread): def run(self): while True: do_updates() time.sleep(600)
2
by: Matt | last post by:
Ok, this is probably a simple and stupid question, but its giving me hell. Lets say that this is my app public class myapp : System.Windows.Forms.Form { public myapp() { }
14
by: Jon W | last post by:
I'm sure that many of you have had problems getting files recognized as html. Now I'm in the reverse position. Text file .txt file, .kook file whatever, IE6 will try and render anything that...
0
by: Matt | last post by:
I'm attempting to load a form, query a database, and update a progress bar on the same form. I want the form to load and activate/enable the form's cancel button while the code continues to...
51
by: Hans | last post by:
Hi all, Is there a way that the program that created and started a thread also stops it. (My usage is a time-out). E.g. thread = threading.Thread(target=Loop.testLoop) thread.start() ...
4
by: sphinney | last post by:
Hi everyone. I'm creating an application inside Access 2007. The application will retrieve data from various locations on my company's network servers. Depending on the time of day, alignment of...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.