Pergunta

Estou no meio da construção de um serviço simples do Windows e estou encontrando um pequeno problema.

O serviço funciona muito bem, o método OnStart cria um processo de trabalhador que ouve as conexões UDP recebidas.

O problema que estou tendo é que, quando clico em parar no serviço ou reiniciar, o serviço permanece em execução no gerenciador de tarefas. Não tenho certeza do que estou fazendo de errado.

Imports System.IO
Imports System.Net.Sockets
Imports System.Net
Imports System.Text

Public Class Service1
Private ListenSocket As New MyNameSpace.Logging
Private wt As System.Threading.Thread

Protected Overrides Sub OnStart(ByVal args() As String)
    AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf UnhandledExceptionEventRaised

    'Load Initial IP Details'
    Try
        Dim logger As New MyNameSpace.Logging
        logger.LoadIPDetails()
    Catch ex As Exception
        MyNameSpace.ErrorLogging.Log(ex)
    End Try

    'Start the listener in a new worker
    Try
        Dim ts As System.Threading.ThreadStart
        ts = AddressOf ListenSocket.ListenForSyslogs
        wt = New System.Threading.Thread(ts)
        wt.Start()
    Catch ex As Exception
        MyNameSpace.ErrorLogging.Log(ex)
    End Try
End Sub

Protected Overrides Sub OnStop()
    ' Add code here to perform any tear-down necessary to stop your service.
    Try
        wt.Abort()
        wt = Nothing

    Catch ex As Exception
        MyNameSpace.ErrorLogging.Log(ex)
    End Try
End Sub

Protected Overloads Sub UnhandledExceptionEventRaised(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
    If e.IsTerminating Then
        Dim o As Object = e.ExceptionObject
        MyNameSpace.ErrorLogging.Log(o) ' use EventLog instead
    End If
End Sub


End Class
Foi útil?

Solução

Eu postei basicamente a mesma pergunta neste fim de semana, você pode querer olhar para a resposta que recebi lá:

Como posso obter meu serviço de ouvinte TCP encerrar corretamente?

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top