我怎样才能让我的TCP监听服务,以正确的终止? [英] How can I get my TCP listener service to terminate correctly?

查看:145
本文介绍了我怎样才能让我的TCP监听服务,以正确的终止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个Windows服务,这将启动一个TCP监听器。核心code正常工作,但我有几个问题,一个Windows服务的机制。

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.

现在,当我的服务启动时,它会创建一个线程并启动TCP监听器的线程。然后,当该服务停止,则终止该线程:

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?

[顺便说一句,我正在做这在VS2010 Beta 2中,如果该事项。]

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

推荐答案

而不是终止与Thread.Abort的(线程),你应该实施一些shutdown()方法的正常关闭套接字。

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

例如。

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

在关机()调用socketListen.Close()和辅助线程正在等待新的连接SocketExcpetion将得到提升。我们就忽略这个问题。
在您的onStop()方法中,首先给pmtListener实例有机会正常关闭致电myListener.Shutdown()(然后设置shutingdown标志和关闭套接字),然后等待(最多)一段时间内(如1秒)。如果线程仍然活着试图终止它。

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

这篇关于我怎样才能让我的TCP监听服务,以正确的终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆