diff --git a/Class/LiteTaskService.vb b/Class/LiteTaskService.vb index b3552a0..091b8e9 100644 --- a/Class/LiteTaskService.vb +++ b/Class/LiteTaskService.vb @@ -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 diff --git a/Class/UpdateManager.vb b/Class/UpdateManager.vb index 72baea4..080d800 100644 --- a/Class/UpdateManager.vb +++ b/Class/UpdateManager.vb @@ -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 diff --git a/Program.vb b/Program.vb index f2ffcc7..9f30bdc 100644 --- a/Program.vb +++ b/Program.vb @@ -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() @@ -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 @@ -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()) @@ -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}