Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 0 additions & 29 deletions Class/LiteTaskService.vb
Original file line number Diff line number Diff line change
Expand Up @@ -569,34 +569,5 @@ Namespace LiteTask
End Using
End Sub

Private Sub RunAsService()
Try
If Not EventLog.SourceExists("LiteTaskService") Then
EventLog.CreateEventSource("LiteTaskService", "Application")
End If

InitializeContainer()

Using service = ApplicationContainer.GetService(Of LiteTaskService)()
EventLog.WriteEntry("LiteTaskService", "Starting service...", EventLogEntryType.Information)

service.EnsureRequiredPermissions()

Dim servicesToRun() As ServiceBase = {service}
ServiceBase.Run(servicesToRun)
End Using

Catch ex As Exception
LogServiceError("Error starting service", ex)
EventLog.WriteEntry("LiteTaskService", $"Error starting service: {ex.Message}", EventLogEntryType.Error)
Throw
Finally
Try
ApplicationContainer.Dispose()
Catch disposeEx As Exception
LogServiceError("Error disposing container", disposeEx)
End Try
End Try
End Sub
End Class
End Namespace
11 changes: 8 additions & 3 deletions Class/UpdateManager.vb
Original file line number Diff line number Diff line change
Expand Up @@ -329,12 +329,17 @@ try {{
try {{
$service = Get-Service -Name 'LiteTaskService' -ErrorAction SilentlyContinue
if ($service) {{
Start-Service -Name 'LiteTaskService' -ErrorAction SilentlyContinue
# Wait briefly for service to start
try {{
Start-Service -Name 'LiteTaskService' -ErrorAction Stop
}} catch {{
# Direct start failed (e.g. insufficient privileges), try elevated
Start-Process powershell -ArgumentList '-NoProfile -Command ""Start-Service LiteTaskService""' -Verb RunAs -WindowStyle Hidden -Wait -ErrorAction Stop
}}
$service.Refresh()
$service.WaitForStatus('Running', (New-TimeSpan -Seconds 30)) | Out-Null
}}
}} catch {{
# Service not installed or failed to start, ignore
# Service not installed, user declined UAC, or failed to start
}}

# Restart the application
Expand Down
54 changes: 23 additions & 31 deletions Program.vb
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,30 @@ Namespace LiteTask
Application.SetCompatibleTextRenderingDefault(False)
End If

' For GUI modes (no args or -elevated), check single instance
' BEFORE initializing the container so a duplicate exits immediately
' without spinning up DI services, timers, etc.
Dim isGuiMode = args.Length = 0 OrElse
(args.Length > 0 AndAlso args(0).Equals("-elevated", StringComparison.OrdinalIgnoreCase))

If isGuiMode Then
Dim createdNew As Boolean
_mutex = New Mutex(True, MutexName, createdNew)
If Not createdNew Then
MessageBox.Show("Another instance of LiteTask is already running.", "LiteTask",
MessageBoxButtons.OK, MessageBoxIcon.Information)
Return
End If
End If

' Initialize container
InitializeContainer()

' Clean up any orphaned temp files from previous runs
Try
Dim logger = ApplicationContainer.GetService(Of Logger)()
logger.CleanupAllTempFiles()

' Also cleanup config files and backups
Dim xmlManager = ApplicationContainer.GetService(Of XMLManager)()
xmlManager.CleanupConfigFiles()
Expand All @@ -53,15 +69,6 @@ Namespace LiteTask
If args.Length > 0 Then
HandleCommandLineArguments(args)
Else
' Check for single instance only in UI mode
Dim createdNew As Boolean
_mutex = New Mutex(True, MutexName, createdNew)
If Not createdNew Then
MessageBox.Show("Another instance of LiteTask is already running.", "LiteTask",
MessageBoxButtons.OK, MessageBoxIcon.Information)
Return
End If

' Create and show the main application context
Application.Run(New ApplicationContext())
End If
Expand Down Expand Up @@ -190,11 +197,9 @@ Namespace LiteTask

Private Sub HandleElevatedMode()
Try
' Perform elevated operations here
_logger?.LogInfo("Application running in elevated mode")

' Initialize with elevated privileges
InitializeContainer()
' Container already initialized in Main(); mutex already acquired
Dim logger = TryGetService(Of Logger)()
logger?.LogInfo("Application running in elevated mode")

' Run the application
Application.Run(New ApplicationContext())
Expand Down Expand Up @@ -389,21 +394,8 @@ Namespace LiteTask
InitializeEventLogSource()
EventLog.WriteEntry(ServiceName, "Starting service...", EventLogEntryType.Information)

' Initialize app with secure defaults
InitializeContainer()

' Clean up any orphaned temp files from previous runs
Try
Dim logger = ApplicationContainer.GetService(Of Logger)()
logger.CleanupAllTempFiles()

' Also cleanup config files and backups
Dim xmlManager = ApplicationContainer.GetService(Of XMLManager)()
xmlManager.CleanupConfigFiles()
Catch ex As Exception
EventLog.WriteEntry(ServiceName, $"Warning: Failed to cleanup temp files: {ex.Message}", EventLogEntryType.Warning)
End Try

' Container and temp-file cleanup already performed in Main()

Dim service = ApplicationContainer.GetService(Of LiteTaskService)()

Dim servicesToRun() As ServiceBase = {service}
Expand Down
Loading