Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 500703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:06:53+00:00 2026-05-13T06:06:53+00:00

I’m writing a Windows service which starts a TCP listener. The core code works

  • 0

I’m writing a Windows service which starts a TCP listener. The core code works fine, but I’m having several problems with the mechanics of a Windows service.

Right now, when my service starts up, it creates a thread and starts the TCP listener in the thread. Then, when the service stops, it terminates that thread:

Public Class txnSocketService
    Inherits System.ServiceProcess.ServiceBase

    Private listenerThread As Thread


    Public Sub New()
        Me.ServiceName = "txnSocketService"
        Me.CanStop = True
        Me.CanPauseAndContinue = True
        Me.AutoLog = True
    End Sub

    Shared Sub Main()
        System.ServiceProcess.ServiceBase.Run(New txnSocketService)
    End Sub

    Protected Overrides Sub OnStart(ByVal args() As String)
        listenerThread = New Thread(AddressOf pmtListener.Main)
        listenerThread.IsBackground = True
        listenerThread.Start()
    End Sub

    Protected Overrides Sub OnStop()
        listenerThread.Abort()
    End Sub

    Private Sub InitializeComponent()
        '
        'txnSocketService
        '
        Me.ServiceName = "txnSocketService"

    End Sub
End Class

Starting up works fine. If I stop the service, however, the service process doesn’t terminate. What am I doing wrong?

[By the way, I’m currently doing this on VS2010 Beta 2, if that matters.]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:06:53+00:00Added an answer on May 13, 2026 at 6:06 am

    Instead of terminating the thread with Thread.Abort() you should implement some shutdown() method that gracefully closes the socket.

    e.g.

    Public Class pmtListener
      Protected shutingdown As Boolean = False
      ' [...] '
      Public Sub Shutdown()
        shutingdown = True
        socketListen.Close()
      End Sub
      Sub Main()
        Dim socketAccepted As Socket
        shutingdown = False
        socketListen.Listen(3)
        While Not shutingdown
          Try
            socketAccepted = socketListen.Accept()
            ' do something with socketAccepted '
            socketAccepted.Close()
            socketAccepted = Nothing
          Catch ex As SocketException
            If shutingdown Then
              ' ignoring it '
            End If
          End Try
    
        End While
      End Sub
    

    When Shutdown() calls socketListen.Close() and the worker thread is currently waiting for a new connection a SocketExcpetion will be raised. We just ignore that.
    In your OnStop() method you first give the pmtListener instance a chance to shutdown gracefully by calling myListener.Shutdown() (which then sets the shutingdown flag and closes the socket) and then wait (up to) a certain timespan (e.g. one second). Should the thread still be alive try to terminate it.

    Public Class txnSocketService
      Inherits System.ServiceProcess.ServiceBase
    
      Protected myListener as pmtListern
      Protected listenerThread As Thread
    
      ' [...] '
      Protected Overrides Sub OnStart(ByVal args() As String)
        myListener = new pmtListener
        listenerThread = New Thread(AddressOf myListener.Main)
        listenerThread.IsBackground = True
        listenerThread.Start()
      End Sub
    
      Protected Overrides Sub OnStop()
        myListener.Shutdown()
        listenerThread.Join(1000) ' give it a second '
        If listenerThread.IsAlive Then
          listenerThread.Abort()
        End If
      End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.