Console application quits unexpectedly

Adagio

Well-known member
Joined
Dec 12, 2005
Messages
162
Programming Experience
Beginner
I just started working on a new console application (sooner or later it will be changed to a service application, but during test it runs as console application)

The problem is the application quits unexpectedly. No exceptions are thrown or anything

I have added many breakpoints and I'm trying to step through the application. I can get to the breakpoint at the line where I call Teknik.CreateExcelFile(), but I never enter the class. I have a breakpoint at the first line in CreateExcelFile, but the code never reaches that point as the application quits before hitting that breakpoint. I have told the debugger to break at any exceptions handled and unhandled
If I remove the Teknik.CreateExcelFile() line, the application runs like it should, so the problem is here, but how do I get the exception? Something happens for sure, but I can't get any information about what happens

Here is all the code relevant to the problem: (Main is the class that is called on startup)

VB.NET:
Public Class Main

    Public Shared Sub Main()
        ServiceTools.StartService(True)
    End Sub

End Class

Module TeknikService

        Private isRunning As Boolean = False
        Private testFlag As Boolean = False

        Public Sub StartService(ByVal test As Boolean)
            If isRunning = True Then Exit Sub

            testFlag = test
            isRunning = True

            Dim thd As New System.Threading.Thread(AddressOf ServiceRun)
            thd.IsBackground = True
            thd.Name = "TeknikService"
            thd.Priority = Threading.ThreadPriority.Lowest
            thd.Start()
        End Sub

        Public Sub StopService()
            isRunning = False
        End Sub

        Private Sub ServiceRun()
            While isRunning = True
                Try
                    Teknik.CreateExcelFile()

                    If testFlag = True Then Exit While

                    System.Threading.Thread.Sleep(1000 * 60 * 5)
                Catch ex As Exception
                    ex = ex
                End Try
            End While
        End Sub

    End Module

Public Module ExcelTools

Public Sub CreateExcelFile()
 Dim excelEngine As ExcelEngine = New ExcelEngine() ' Some Syncfusion Excel tools
End Sub

 End Module
 
I just tried to delete pretty much anything in the ExcelTools module and the problem still occurs when calling it

In the ExcelTools module I only have the CreateExcelFile sub which now looks like this:

VB.NET:
Public Sub CreateExcelFile()
            Try
                Dim i As Integer = 0
            Catch ex As Exception
                ex = ex
            End Try
End Sub

The breakpoint at Dim i as Integer is never reached


Weird, after several restarts without changing any code it started to reach the breakpoint, but only if I don't have the following line in the sub:
Dim excelEngine As ExcelEngine = New ExcelEngine() ' Some Syncfusion Excel tools

But I still can't get it to throw an exception. I'm using these tools often in my other projects, which makes me wonder why it doesn't work here
 
Last edited:
Your console Main starts a background thread and ends. This ends the application run and all background threads are also stopped, so your background thread never gets to do anything. A foreground thread is needed to keep the app alive. Foreground and Background Threads

A service operates more like a windows application that stays alive until it is stopped regardless of whether it is doing anything.
 
Back
Top