App not shutting down on system reboot End Now message

TyB

Well-known member
Joined
May 14, 2009
Messages
102
Programming Experience
3-5
I wrote a program that is a system tray app that has no user interface. The issue is that when you shutdown, log off, or restart the PC the app does not shutdown and the user has the "End Now" message box pop up.

The only thing running on this app is that it is listening to a port for communication.

I do not have any code in the form closing event as that is what I want just to close.

Any thoughts?

Thanks,

Ty
 
You must have started a new foreground thread that you're not stopping when requested to.
 
Not sure

The only thing that could have started a new thread was the tcplistner.

Here is essentially the code that runs on load.

VB.NET:
    Private Sub GetMessage()
        ' Must listen on correct port- must be same as port client wants to connect on.
        Const portNumber As Integer = 8000
        Dim tcpListener As New TcpListener(portNumber)
        Dim intLen As Integer
        Dim intThere As Integer
        Dim intThere2 As Integer
        Dim strType As String
        Dim strHeader As String
        Dim strMessage As String

        tcpListener.Start()

        Try

            Me.Hide()
            
            'Accept the pending client connection and return             
            'a TcpClient initialized for communication. 
            Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
            
            ' Get the stream
            Dim networkStream As NetworkStream = tcpClient.GetStream()
            
            ' Read the stream into a byte array
            Dim bytes(tcpClient.ReceiveBufferSize) As Byte
            networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))

            ' Return the data received from the client to the console.
            Dim clientdata As String = Encoding.ASCII.GetString(bytes)

            'Now we have to parse out the type of message, Header and message
            intLen = Len(clientdata)

            'Get the message type.
            intThere = InStr(clientdata, "~")
            strType = Mid(clientdata, 1, 1)

            'Set the form colors
            If strType = "2" Then 'Critical
                TextBox1.ForeColor = Color.White
                TextBox1.BackColor = Color.DarkRed
                PictureBox1.Visible = True
                PictureBox2.Visible = False

            Else 'Informational
                TextBox1.ForeColor = Color.Black
                TextBox1.BackColor = Color.Gold
                PictureBox2.Visible = True
                PictureBox1.Visible = False

            End If

            'Get the Window Size
            strType = Mid(clientdata, 3, 1)

            'Get Header text
            intThere2 = InStrRev(clientdata, "~", intLen)
            strHeader = Mid(clientdata, 5, intThere2 - (4 + 1))

            'Get Message
            strMessage = Mid(clientdata, intThere2 + 1, intLen - intThere2)

            TextBox1.Text = strHeader
            txtData.Text = strMessage

            
            'Any communication with the remote client using the TcpClient can go here.
            'Close TcpListener and TcpClient.
            tcpClient.Close()
            tcpListener.Stop()
            Me.Show()

            'Set the global variable that will be used in the timing.
            intTick = 0

            'Start the timer that will close the form after 5 minutes.
            Timer2.Enabled = True


            strType = Mid(clientdata, 3, 1)

            If strType = "1" Then
                Me.WindowState = FormWindowState.Normal
            Else
                Me.WindowState = FormWindowState.Minimized
            End If

            Timer1.Enabled = True


        Catch ex As Exception
            Dim strerror As String
            strerror = ex.ToString
            MessageBox.Show(strerror, "Error getting message. Please call the Helpdesk", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try
    End Sub
 
...or doing blocking calls in UI thread. It is unacceptable to run the code you posted in the UI thread. In particular this goes for the AcceptTcpClient when no connection is pending, but also reading from NetworkStream in UI thread is dubious.
 
Well

The code I posted runs only when a incoming message is received. the tcplisten is the only thing running constantly.

Ty
 
Tried

After thinking on your reply I moved the tcplisten code to a new thread and placed all the data manipulation code into the delegate for that thread and place the thread abort in the form closing event hoping that with it on another thread the base form would get the close notification, but seems that it did not make any difference at all.

Ty
 
Back
Top