473,748 Members | 6,037 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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()
InitializeCompo nent() ' This call is required by the Windows Form
Designer.
m_thTCP = New Thread(AddressO f TCPServer)
ThreadStart()
End Sub

Private Sub Form1_FormClosi ng(ByVal sender As Object, ByVal e As
System.Windows. Forms.FormClosi ngEventArgs) Handles Me.FormClosing
ThreadStop()
End Sub

Private Sub Restart_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles RestartButton.C lick
ThreadRestart()
End Sub

Private Sub ThreadStop() ' Stopping TCP Listener Service
Try
If (m_thTCP.Thread State <ThreadState.St opped) Then
Dim i As Int16 = 0
m_listener.Stop ()
Application.DoE vents()
Thread.Sleep(20 0)
While ((i < 100) And (m_thTCP.Thread State <ThreadState.St opped))
' gives thread 5 secs to stop!
m_thTCP.Abort()
Application.DoE vents()
Thread.Sleep(50 )
i += 1
End While
End If
Catch ex As Exception
MsgBox(ex.Messa ge)
Finally
Console.WriteLi ne("ThreadStat e is {0}", m_thTCP.ThreadS tate.ToString() )
m_thTCP = Nothing
End Try
End Sub

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

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

Private Sub TCPServer()
Try ' Starting TCP Listener from thread
m_listener = New TcpListener(IPA ddress.Any, m_port)
m_listener.Star t()
While True
Dim client As TcpClient = m_listener.Acce ptTcpClient()
' Waits until data is available on the network
Dim stream As NetworkStream = client.GetStrea m()
Dim bytes(client.Re ceiveBufferSize ) As Byte
stream.Read(byt es, 0, CInt(client.Rec eiveBufferSize) )
Dim data As String = Encoding.ASCII. GetString(bytes )
data = data.TrimEnd(da ta.Substring(da ta.Length - 1))
If (data.Substring (3) <String.Empty ) Then
Dim item As String = data.Substring( 3)
If (item <"") Then
Console.WriteLi ne("Data Read: {0}", item)
End If
End If
stream.Close()
client.Close()
End While
Catch ex2 As SocketException
Console.WriteLi ne("SocketExcep tion: {0}", ex2)
Catch ex1 As ThreadAbortExce ption ' dismiss this one
Exit Sub
Catch ex As Exception
MsgBox(ex.Messa ge)
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 1324

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

Similar topics

9
2416
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
234
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
1928
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 looks like a tag. I've made a page with frames (for shame) and I've specified txt files as the src for the purpose of viewing the source. NN7.1 will display text in the frames. IE will not. I've tried poping headers through perl/web server;...
0
1182
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 execute and the progress bar updates. The user will be able to press the cancel button to stop execution. Everything works fine, but I've noticed that when I call thread.Abort, an exception is thrown: "Thread was being aborted." Is there a better...
51
54860
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() # This thread is expected to finish within a second
4
27987
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 the planets, other unfathomable mysteries sometimes my company's network is very, very slow. I would like to provide the user of my application with a "Cancel" button on a form that will cancel/stop execution of the code (at whatever point it may...
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8243
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4606
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.