From 27792dc8a3439692a57704a48d9de625d3f428fd Mon Sep 17 00:00:00 2001 From: MyDrift Date: Sat, 13 Jul 2024 12:54:59 +0200 Subject: [PATCH] use Dispatcher.Invoke --- functions/private/Install-WinUtilChoco.ps1 | 6 +++--- functions/private/Install-WinUtilProgramChoco.ps1 | 2 +- functions/private/Install-WinUtilProgramWinget.ps1 | 2 +- functions/private/Install-WinUtilWinget.ps1 | 4 ++-- functions/private/Invoke-WinUtilFeatureInstall.ps1 | 8 +++----- functions/public/Invoke-WPFGetInstalled.ps1 | 4 ++-- functions/public/Invoke-WPFInstall.ps1 | 6 +++--- functions/public/Invoke-WPFUnInstall.ps1 | 6 +++--- functions/public/Invoke-WPFtweaksbutton.ps1 | 3 +++ functions/public/Invoke-WPFundoall.ps1 | 2 ++ 10 files changed, 23 insertions(+), 20 deletions(-) diff --git a/functions/private/Install-WinUtilChoco.ps1 b/functions/private/Install-WinUtilChoco.ps1 index 718855eb..7c026945 100644 --- a/functions/private/Install-WinUtilChoco.ps1 +++ b/functions/private/Install-WinUtilChoco.ps1 @@ -14,19 +14,19 @@ function Install-WinUtilChoco { return } - Set-WinUtilTaskbaritem -state "Indeterminate" + $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Indeterminate" }) Write-Host "Seems Chocolatey is not installed, installing now." Set-ExecutionPolicy Bypass -Scope Process -Force; Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')) -ErrorAction Stop powershell choco feature enable -n allowGlobalConfirmation - Set-WinUtilTaskbaritem -state "None" + $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "None" }) } Catch { Write-Host "===========================================" -Foregroundcolor Red Write-Host "-- Chocolatey failed to install ---" -Foregroundcolor Red Write-Host "===========================================" -Foregroundcolor Red - Set-WinUtilTaskbaritem -state "Error" + $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Error" }) } } diff --git a/functions/private/Install-WinUtilProgramChoco.ps1 b/functions/private/Install-WinUtilProgramChoco.ps1 index 58381f9d..b487f91b 100644 --- a/functions/private/Install-WinUtilProgramChoco.ps1 +++ b/functions/private/Install-WinUtilProgramChoco.ps1 @@ -80,7 +80,7 @@ function Install-WinUtilProgramChoco { } } $x++ - # Set-WinUtilTaskbaritem -state "Normal" -value $x/$count + # $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Normal" -value $using:x/$using:count }) } Write-Progress -Activity "$manage Applications" -Status "Finished" -Completed diff --git a/functions/private/Install-WinUtilProgramWinget.ps1 b/functions/private/Install-WinUtilProgramWinget.ps1 index a3cddef2..7abc9e39 100644 --- a/functions/private/Install-WinUtilProgramWinget.ps1 +++ b/functions/private/Install-WinUtilProgramWinget.ps1 @@ -97,7 +97,7 @@ Function Install-WinUtilProgramWinget { } } $X++ - # Set-WinUtilTaskbaritem -state "Normal" -value $x/$count + # $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Normal" -value $using:x/$using:count }) } Write-Progress -Activity "$manage Applications" -Status "Finished" -Completed return $failedPackages; diff --git a/functions/private/Install-WinUtilWinget.ps1 b/functions/private/Install-WinUtilWinget.ps1 index 3e8aa7b6..28a761b9 100644 --- a/functions/private/Install-WinUtilWinget.ps1 +++ b/functions/private/Install-WinUtilWinget.ps1 @@ -19,7 +19,7 @@ function Install-WinUtilWinget { Write-Host "`nWinget is not Installed. Continuing with install.`r" -ForegroundColor Red } - Set-WinUtilTaskbaritem -state "Indeterminate" + $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Indeterminate" }) # Gets the computer's information if ($null -eq $sync.ComputerInfo){ @@ -65,6 +65,6 @@ function Install-WinUtilWinget { throw [WingetFailedInstall]::new('Failed to install!') } } - Set-WinUtilTaskbaritem -state "None" + $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "None" }) } diff --git a/functions/private/Invoke-WinUtilFeatureInstall.ps1 b/functions/private/Invoke-WinUtilFeatureInstall.ps1 index 1784c315..08839498 100644 --- a/functions/private/Invoke-WinUtilFeatureInstall.ps1 +++ b/functions/private/Invoke-WinUtilFeatureInstall.ps1 @@ -10,7 +10,7 @@ function Invoke-WinUtilFeatureInstall { $CheckBox ) - Set-WinUtilTaskbaritem -state "Normal" -value 1 + # $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Normal" -value 1/$using:CheckBox.Count }) $CheckBox | ForEach-Object { if($sync.configs.feature.$psitem.feature){ @@ -39,6 +39,7 @@ function Invoke-WinUtilFeatureInstall { Write-Host "Running Script for $psitem" Invoke-Command $scriptblock -ErrorAction stop + $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "None" }) } Catch{ if ($psitem.Exception.Message -like "*requires elevation*"){ @@ -46,7 +47,7 @@ function Invoke-WinUtilFeatureInstall { } else{ - Set-WinUtilTaskbaritem -state "Error" + $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Error" }) Write-Warning "Unable to Install $feature due to unhandled exception" Write-Warning $psitem.Exception.StackTrace } @@ -54,7 +55,4 @@ function Invoke-WinUtilFeatureInstall { } } } - if ($sync["Form"].taskbarItemInfo.ProgressState -ne "Error"){ - Set-WinUtilTaskbaritem -state "None" - } } diff --git a/functions/public/Invoke-WPFGetInstalled.ps1 b/functions/public/Invoke-WPFGetInstalled.ps1 index 76aaa47d..9ef1c3ac 100644 --- a/functions/public/Invoke-WPFGetInstalled.ps1 +++ b/functions/public/Invoke-WPFGetInstalled.ps1 @@ -24,7 +24,7 @@ function Invoke-WPFGetInstalled { param($checkbox, $DebugPreference) $sync.ProcessRunning = $true - Set-WinUtilTaskbaritem -state "Indeterminate" + $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Indeterminate" }) if($checkbox -eq "winget"){ Write-Host "Getting Installed Programs..." @@ -43,6 +43,6 @@ function Invoke-WPFGetInstalled { Write-Host "Done..." $sync.ProcessRunning = $false - Set-WinUtilTaskbaritem -state "None" + $sync.form.Dispatcher.Invoke([action] { Set-WinUtilTaskbaritem -state "None" }) } } diff --git a/functions/public/Invoke-WPFInstall.ps1 b/functions/public/Invoke-WPFInstall.ps1 index b3c969ca..e9eccebb 100644 --- a/functions/public/Invoke-WPFInstall.ps1 +++ b/functions/public/Invoke-WPFInstall.ps1 @@ -23,7 +23,7 @@ function Invoke-WPFInstall { Invoke-WPFRunspace -ArgumentList $PackagesToInstall -DebugPreference $DebugPreference -ScriptBlock { param($PackagesToInstall, $DebugPreference) - Set-WinUtilTaskbaritem -state "Normal" -value 1/$PackagesToInstall.Count + # $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Normal" -value 1/$using:PackagesToInstall.Count }) $packagesWinget, $packagesChoco = { $packagesWinget = [System.Collections.Generic.List`1[System.Object]]::new() $packagesChoco = [System.Collections.Generic.List`1[System.Object]]::new() @@ -54,13 +54,13 @@ function Invoke-WPFInstall { Write-Host "===========================================" Write-Host "-- Installs have finished ---" Write-Host "===========================================" - # Set-WinUtilTaskbaritem -state "None" + $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "None" }) } Catch { Write-Host "===========================================" Write-Host "Error: $_" Write-Host "===========================================" - # Set-WinUtilTaskbaritem -state "Error" + $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Error" }) } Start-Sleep -Seconds 5 $sync.ProcessRunning = $False diff --git a/functions/public/Invoke-WPFUnInstall.ps1 b/functions/public/Invoke-WPFUnInstall.ps1 index 800af84a..bc8fcad4 100644 --- a/functions/public/Invoke-WPFUnInstall.ps1 +++ b/functions/public/Invoke-WPFUnInstall.ps1 @@ -29,10 +29,10 @@ function Invoke-WPFUnInstall { if($confirm -eq "No"){return} - Set-WinUtilTaskbaritem -state "Normal" -value 1/$PackagesToInstall.Count Invoke-WPFRunspace -ArgumentList $PackagesToInstall -DebugPreference $DebugPreference -ScriptBlock { param($PackagesToInstall, $DebugPreference) + # $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Normal" -value 1/$using:PackagesToInstall.Count }) $packagesWinget, $packagesChoco = { $packagesWinget = [System.Collections.Generic.List`1[System.Object]]::new() $packagesChoco = [System.Collections.Generic.List`1[System.Object]]::new() @@ -68,13 +68,13 @@ function Invoke-WPFUnInstall { Write-Host "===========================================" Write-Host "-- Uninstalls have finished ---" Write-Host "===========================================" - # Set-WinUtilTaskbaritem -state "None" + $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "None" }) } Catch { Write-Host "===========================================" Write-Host "Error: $_" Write-Host "===========================================" - # Set-WinUtilTaskbaritem -state "Error" + $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Error" }) } $sync.ProcessRunning = $False } diff --git a/functions/public/Invoke-WPFtweaksbutton.ps1 b/functions/public/Invoke-WPFtweaksbutton.ps1 index 9eb60cf2..1fffd33c 100644 --- a/functions/public/Invoke-WPFtweaksbutton.ps1 +++ b/functions/public/Invoke-WPFtweaksbutton.ps1 @@ -30,15 +30,18 @@ function Invoke-WPFtweaksbutton { $sync.ProcessRunning = $true + # $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Normal" -value 1/$Tweaks.Count }) $cnt = 0 # Execute other selected tweaks foreach ($tweak in $Tweaks) { Write-Debug "This is a tweak to run $tweak count: $cnt" Invoke-WinUtilTweaks $tweak $cnt += 1 + # $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -value ($using:cnt/$using:Tweaks.Count)}) } $sync.ProcessRunning = $false + $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "None" }) Write-Host "=================================" Write-Host "-- Tweaks are Finished ---" Write-Host "=================================" diff --git a/functions/public/Invoke-WPFundoall.ps1 b/functions/public/Invoke-WPFundoall.ps1 index 9d961f6e..0e5cfe60 100644 --- a/functions/public/Invoke-WPFundoall.ps1 +++ b/functions/public/Invoke-WPFundoall.ps1 @@ -24,12 +24,14 @@ function Invoke-WPFundoall { param($Tweaks, $DebugPreference) $sync.ProcessRunning = $true + # $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Normal" -value $using:Tweaks.Count }) Foreach ($tweak in $tweaks){ Invoke-WinUtilTweaks $tweak -undo $true } $sync.ProcessRunning = $false + $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "None" }) Write-Host "==================================" Write-Host "--- Undo Tweaks are Finished ---" Write-Host "=================================="