Compare commits

..

1 Commits

Author SHA1 Message Date
9d5e681aac Runspace Change to fix Tweaks 2024-09-11 14:22:01 -05:00
7 changed files with 248 additions and 222 deletions

View File

@ -104,7 +104,7 @@
}, },
"Matrix": { "Matrix": {
"ComboBoxBackgroundColor": "#232629", "ComboBoxBackgroundColor": "#232629",
"LabelboxForegroundColor": "#f7f7f7", "LabelboxForegroundColor": "#81a1c1",
"MainForegroundColor": "#F7F7F7", "MainForegroundColor": "#F7F7F7",
"MainBackgroundColor": "#232629", "MainBackgroundColor": "#232629",
"LabelBackgroundColor": "#232629", "LabelBackgroundColor": "#232629",
@ -136,14 +136,14 @@
"ShadowPulse": "0:0:3" "ShadowPulse": "0:0:3"
}, },
"Dark": { "Dark": {
"ComboBoxBackgroundColor": "#1e3747", "ComboBoxBackgroundColor": "#232629",
"LabelboxForegroundColor": "#0567ff", "LabelboxForegroundColor": "#81a1c1",
"MainForegroundColor": "#F7F7F7", "MainForegroundColor": "#F7F7F7",
"MainBackgroundColor": "#121212", "MainBackgroundColor": "#232629",
"LabelBackgroundColor": "#121212", "LabelBackgroundColor": "#232629",
"LinkForegroundColor": "#add8e6", "LinkForegroundColor": "#add8e6",
"LinkHoverForegroundColor": "#F7F7F7", "LinkHoverForegroundColor": "#F7F7F7",
"ComboBoxForegroundColor": "#f7f7f7", "ComboBoxForegroundColor": "#81a1c1",
"ProgressBarForegroundColor": "#222222", "ProgressBarForegroundColor": "#222222",
"ProgressBarBackgroundColor": "Transparent", "ProgressBarBackgroundColor": "Transparent",
@ -164,6 +164,6 @@
"ButtonForegroundColor": "#F7F7F7", "ButtonForegroundColor": "#F7F7F7",
"ToggleButtonOnColor": "#2e77ff", "ToggleButtonOnColor": "#2e77ff",
"BorderColor": "#2F373D" "BorderColor": "#0b1215"
} }
} }

View File

@ -13,11 +13,10 @@ function Install-WinUtilChoco {
if((Test-WinUtilPackageManager -choco) -eq "installed") { if((Test-WinUtilPackageManager -choco) -eq "installed") {
return return
} }
# Install logic taken from https://chocolatey.org/install#individual
Write-Host "Seems Chocolatey is not installed, installing now." Write-Host "Seems Chocolatey is not installed, installing now."
Set-ExecutionPolicy Bypass -Scope Process -Force; Start-Process -FilePath "powershell" -ArgumentList "Set-ExecutionPolicy Bypass -Scope Process -Force; Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')) -ErrorAction Stop" -Wait -NoNewWindow
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; Start-Process -FilePath "powershell" -ArgumentList "choco feature enable -n allowGlobalConfirmation" -Wait -NoNewWindow
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
} catch { } catch {
Write-Host "===========================================" -Foregroundcolor Red Write-Host "===========================================" -Foregroundcolor Red

View File

@ -20,7 +20,7 @@ function Invoke-WPFInstall {
return return
} }
$ChocoPreference = $($sync.WPFpreferChocolatey.IsChecked) $ChocoPreference = $($sync.WPFpreferChocolatey.IsChecked)
$installHandle = Invoke-WPFRunspace -ParameterList @(("PackagesToInstall", $PackagesToInstall),("ChocoPreference", $ChocoPreference)) -DebugPreference $DebugPreference -ScriptBlock { Invoke-WPFRunspace -ArgumentList $PackagesToInstall,$ChocoPreference -DebugPreference $DebugPreference -ScriptBlock {
param($PackagesToInstall, $ChocoPreference, $DebugPreference) param($PackagesToInstall, $ChocoPreference, $DebugPreference)
if ($PackagesToInstall.count -eq 1) { if ($PackagesToInstall.count -eq 1) {
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" }) $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" })

View File

@ -11,50 +11,61 @@ function Invoke-WPFRunspace {
.PARAMETER ArgumentList .PARAMETER ArgumentList
A list of arguments to pass to the runspace A list of arguments to pass to the runspace
.PARAMETER ParameterList
A list of named parameters that should be provided.
.EXAMPLE .EXAMPLE
Invoke-WPFRunspace ` Invoke-WPFRunspace `
-ScriptBlock $sync.ScriptsInstallPrograms ` -ScriptBlock $sync.ScriptsInstallPrograms `
-ArgumentList "Installadvancedip,Installbitwarden" ` -ArgumentList "Installadvancedip,Installbitwarden" `
Invoke-WPFRunspace`
-ScriptBlock $sync.ScriptsInstallPrograms `
-ParameterList @(("PackagesToInstall", @("Installadvancedip,Installbitwarden")),("ChocoPreference", $true))
#> #>
[CmdletBinding()] [CmdletBinding()]
Param ( Param (
$ScriptBlock, [Parameter(Mandatory=$true)]
$ArgumentList, [scriptblock]$ScriptBlock,
$ParameterList, [Parameter(Mandatory=$false)]
$DebugPreference [object[]]$ArgumentList,
[Parameter(Mandatory=$false)]
[System.Management.Automation.ActionPreference]$DebugPreference = 'SilentlyContinue'
) )
try {
# Create a PowerShell instance # Create a PowerShell instance
$script:powershell = [powershell]::Create() $powershell = [powershell]::Create()
# Add Scriptblock and Arguments to runspace # Add Scriptblock and Arguments to runspace
$script:powershell.AddScript($ScriptBlock) $powershell.AddScript($ScriptBlock)
$script:powershell.AddArgument($ArgumentList) if ($ArgumentList) {
foreach ($Argument in $ArgumentList) {
foreach ($parameter in $ParameterList) { $powershell.AddArgument($Argument)
$script:powershell.AddParameter($parameter[0], $parameter[1])
} }
$script:powershell.AddArgument($DebugPreference) # Pass DebugPreference to the script block }
$script:powershell.RunspacePool = $sync.runspace $powershell.AddArgument($DebugPreference)
# Execute the RunspacePool # Ensure runspace pool is available
$script:handle = $script:powershell.BeginInvoke() if (-not $sync.runspace -or $sync.runspace.IsDisposed) {
throw "Runspace pool is not initialized or has been disposed."
}
$powershell.RunspacePool = $sync.runspace
# Clean up the RunspacePool threads when they are complete, and invoke the garbage collector to clean up the memory # Execute the RunspacePool asynchronously
if ($script:handle.IsCompleted) { $handle = $powershell.BeginInvoke()
$script:powershell.EndInvoke($script:handle)
$script:powershell.Dispose() # Set up an event to handle completion
$sync.runspace.Dispose() $null = Register-ObjectEvent -InputObject $powershell -EventName InvocationStateChanged -Action {
$sync.runspace.Close() if ($EventArgs.InvocationStateInfo.State -eq "Completed") {
$powershell.EndInvoke($handle)
$powershell.Dispose()
[System.GC]::Collect() [System.GC]::Collect()
Unregister-Event -SourceIdentifier $EventSubscriber.SourceIdentifier
} }
}
# Return the handle # Return the handle
return $handle return $handle
} }
catch {
Write-Error "Error in Invoke-WPFRunspace: $_"
if ($powershell) { $powershell.Dispose() }
throw
}
}

View File

@ -30,7 +30,7 @@ function Invoke-WPFUnInstall {
if($confirm -eq "No") {return} if($confirm -eq "No") {return}
$ChocoPreference = $($sync.WPFpreferChocolatey.IsChecked) $ChocoPreference = $($sync.WPFpreferChocolatey.IsChecked)
Invoke-WPFRunspace -ArgumentList @(("PackagesToInstall", $PackagesToInstall),("ChocoPreference", $ChocoPreference)) -DebugPreference $DebugPreference -ScriptBlock { Invoke-WPFRunspace -ArgumentList $PackagesToInstall, $ChocoPreference -DebugPreference $DebugPreference -ScriptBlock {
param($PackagesToInstall, $ChocoPreference, $DebugPreference) param($PackagesToInstall, $ChocoPreference, $DebugPreference)
if ($PackagesToInstall.count -eq 1) { if ($PackagesToInstall.count -eq 1) {
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" }) $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" })

View File

@ -13,50 +13,66 @@ function Invoke-WPFtweaksbutton {
} }
$Tweaks = (Get-WinUtilCheckBoxes)["WPFTweaks"] $Tweaks = (Get-WinUtilCheckBoxes)["WPFTweaks"]
$DNSChange = $sync["WPFchangedns"].text -ne "Default"
if ($DNSChange) {
Set-WinUtilDNS -DNSProvider $sync["WPFchangedns"].text Set-WinUtilDNS -DNSProvider $sync["WPFchangedns"].text
}
if ($tweaks.count -eq 0 -and $sync["WPFchangedns"].text -eq "Default") { if ($tweaks.count -eq 0 -and -not $DNSChange) {
$msg = "Please check the tweaks you wish to perform." $msg = "Please check the tweaks you wish to perform or select a DNS provider."
[System.Windows.MessageBox]::Show($msg, "Winutil", [System.Windows.MessageBoxButton]::OK, [System.Windows.MessageBoxImage]::Warning) [System.Windows.MessageBox]::Show($msg, "Winutil", [System.Windows.MessageBoxButton]::OK, [System.Windows.MessageBoxImage]::Warning)
return return
} }
Write-Debug "Number of tweaks to process: $($Tweaks.Count)" Write-Debug "Number of tweaks to process: $($Tweaks.Count)"
# The leading "," in the ParameterList is nessecary because we only provide one argument and powershell cannot be convinced that we want a nested loop with only one argument otherwise try {
$tweaksHandle = Invoke-WPFRunspace -ParameterList @(,("tweaks",$tweaks)) -DebugPreference $DebugPreference -ScriptBlock { $handle = Invoke-WPFRunspace -ArgumentList $Tweaks, $DNSChange -DebugPreference $DebugPreference -ScriptBlock {
param( param($Tweaks, $DNSChange, $DebugPreference)
$tweaks,
$DebugPreference
)
Write-Debug "Inside Number of tweaks to process: $($Tweaks.Count)" Write-Debug "Inside Number of tweaks to process: $($Tweaks.Count)"
$sync.ProcessRunning = $true $sync.ProcessRunning = $true
if ($Tweaks.count -eq 1) { if ($Tweaks.count -eq 0 -and $DNSChange) {
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" })
} elseif ($Tweaks.count -eq 1) {
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" }) $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" })
} else { } else {
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Normal" -value 0.01 -overlay "logo" }) $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Normal" -value 0.01 -overlay "logo" })
} }
# Execute other selected tweaks
# Execute selected tweaks
for ($i = 0; $i -lt $Tweaks.Count; $i++) { for ($i = 0; $i -lt $Tweaks.Count; $i++) {
Set-WinUtilProgressBar -Label "Applying $($tweaks[$i])" -Percent ($i / $tweaks.Count * 100) $currentTweak = $Tweaks[$i]
Invoke-WinUtilTweaks $tweaks[$i] Set-WinUtilProgressBar -Label "Applying $currentTweak" -Percent ($i / $Tweaks.Count * 100)
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -value ($i/$Tweaks.Count) })
# Apply the current tweak
Invoke-WinUtilTweaks $currentTweak
# Update taskbar progress
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -value (($i + 1) / $Tweaks.Count) })
} }
Set-WinUtilProgressBar -Label "Tweaks finished" -Percent 100 Set-WinUtilProgressBar -Label "Tweaks finished" -Percent 100
$sync.ProcessRunning = $false $sync.ProcessRunning = $false
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "None" -overlay "checkmark" }) $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "None" -overlay "checkmark" })
Write-Host "=================================" Write-Host "================================="
Write-Host "-- Tweaks are Finished ---" Write-Host "-- Tweaks are Finished ---"
Write-Host "=================================" Write-Host "================================="
}
# $ButtonType = [System.Windows.MessageBoxButton]::OK # Optionally, you can add code here to update the UI or perform other tasks while the runspace is executing
# $MessageboxTitle = "Tweaks are Finished " # For example, you might want to disable certain UI elements until the runspace completes
# $Messageboxbody = ("Done")
# $MessageIcon = [System.Windows.MessageBoxImage]::Information # If you need to wait for completion before proceeding, you can use:
# [System.Windows.MessageBox]::Show($Messageboxbody, $MessageboxTitle, $ButtonType, $MessageIcon) # $handle.AsyncWaitHandle.WaitOne()
# But be cautious about blocking the UI thread
Write-Host "Tweaks execution started in background."
}
catch {
Write-Error "Failed to start tweaks execution: $_"
[System.Windows.MessageBox]::Show("An error occurred while starting tweaks execution. Please check the logs for more information.", "Error", [System.Windows.MessageBoxButton]::OK, [System.Windows.MessageBoxImage]::Error)
} }
} }

View File

@ -911,19 +911,19 @@
<Border Grid.Row="0" Grid.Column="0" Style="{StaticResource BorderStyle}"> <Border Grid.Row="0" Grid.Column="0" Style="{StaticResource BorderStyle}">
<StackPanel Background="{MainBackgroundColor}" SnapsToDevicePixels="True"> <StackPanel Background="{MainBackgroundColor}" SnapsToDevicePixels="True">
<Button Name="WPFUpdatesdefault" FontSize="{ConfigTabButtonFontSize}" Height="Auto" Width="Auto" Content="Default (Out of Box) Settings" Margin="20,4,20,10" Padding="10"/> <Button Name="WPFUpdatesdefault" FontSize="{ConfigTabButtonFontSize}" Height="Auto" Width="Auto" Content="Default (Out of Box) Settings" Margin="20,4,20,10" Padding="10"/>
<TextBlock Foreground="{ComboBoxForegroundColor}" Margin="20,0,20,0" Padding="10" TextWrapping="WrapWithOverflow" MaxWidth="300">This is the default settings that come with Windows. <LineBreak/><LineBreak/> No modifications are made and will remove any custom windows update settings.<LineBreak/><LineBreak/>Note: If you still encounter update errors, reset all updates in the config tab. That will restore ALL Microsoft Update Services from their servers and reinstall them to default settings.</TextBlock> <TextBlock Margin="20,0,20,0" Padding="10" TextWrapping="WrapWithOverflow" MaxWidth="300">This is the default settings that come with Windows. <LineBreak/><LineBreak/> No modifications are made and will remove any custom windows update settings.<LineBreak/><LineBreak/>Note: If you still encounter update errors, reset all updates in the config tab. That will restore ALL Microsoft Update Services from their servers and reinstall them to default settings.</TextBlock>
</StackPanel> </StackPanel>
</Border> </Border>
<Border Grid.Row="0" Grid.Column="1" Style="{StaticResource BorderStyle}"> <Border Grid.Row="0" Grid.Column="1" Style="{StaticResource BorderStyle}">
<StackPanel Background="{MainBackgroundColor}" SnapsToDevicePixels="True"> <StackPanel Background="{MainBackgroundColor}" SnapsToDevicePixels="True">
<Button Name="WPFUpdatessecurity" FontSize="{ConfigTabButtonFontSize}" Height="Auto" Width="Auto" Content="Security (Recommended) Settings" Margin="20,4,20,10" Padding="10"/> <Button Name="WPFUpdatessecurity" FontSize="{ConfigTabButtonFontSize}" Height="Auto" Width="Auto" Content="Security (Recommended) Settings" Margin="20,4,20,10" Padding="10"/>
<TextBlock Foreground="{ComboBoxForegroundColor}" Margin="20,0,20,0" Padding="10" TextWrapping="WrapWithOverflow" MaxWidth="300">This is my recommended setting I use on all computers.<LineBreak/><LineBreak/> It will delay feature updates by 2 years and will install security updates 4 days after release.<LineBreak/><LineBreak/>Feature Updates: Adds features and often bugs to systems when they are released. You want to delay these as long as possible.<LineBreak/><LineBreak/>Security Updates: Typically these are pressing security flaws that need to be patched quickly. You only want to delay these a couple of days just to see if they are safe and don't break other systems. You don't want to go without these for ANY extended periods of time.</TextBlock> <TextBlock Margin="20,0,20,0" Padding="10" TextWrapping="WrapWithOverflow" MaxWidth="300">This is my recommended setting I use on all computers.<LineBreak/><LineBreak/> It will delay feature updates by 2 years and will install security updates 4 days after release.<LineBreak/><LineBreak/>Feature Updates: Adds features and often bugs to systems when they are released. You want to delay these as long as possible.<LineBreak/><LineBreak/>Security Updates: Typically these are pressing security flaws that need to be patched quickly. You only want to delay these a couple of days just to see if they are safe and don't break other systems. You don't want to go without these for ANY extended periods of time.</TextBlock>
</StackPanel> </StackPanel>
</Border> </Border>
<Border Grid.Row="0" Grid.Column="2" Style="{StaticResource BorderStyle}"> <Border Grid.Row="0" Grid.Column="2" Style="{StaticResource BorderStyle}">
<StackPanel Background="{MainBackgroundColor}" SnapsToDevicePixels="True"> <StackPanel Background="{MainBackgroundColor}" SnapsToDevicePixels="True">
<Button Name="WPFUpdatesdisable" FontSize="{ConfigTabButtonFontSize}" Height="Auto" Width="Auto" Content="Disable ALL Updates (NOT RECOMMENDED!)" Margin="20,4,20,10" Padding="10,10,10,10"/> <Button Name="WPFUpdatesdisable" FontSize="{ConfigTabButtonFontSize}" Height="Auto" Width="Auto" Content="Disable ALL Updates (NOT RECOMMENDED!)" Margin="20,4,20,10" Padding="10,10,10,10"/>
<TextBlock Foreground="{ComboBoxForegroundColor}" Margin="20,0,20,0" Padding="10" TextWrapping="WrapWithOverflow" MaxWidth="300">This completely disables ALL Windows Updates and is NOT RECOMMENDED.<LineBreak/><LineBreak/> However, it can be suitable if you use your system for a select purpose and do not actively browse the internet. <LineBreak/><LineBreak/>Note: Your system will be easier to hack and infect without security updates.</TextBlock> <TextBlock Margin="20,0,20,0" Padding="10" TextWrapping="WrapWithOverflow" MaxWidth="300">This completely disables ALL Windows Updates and is NOT RECOMMENDED.<LineBreak/><LineBreak/> However, it can be suitable if you use your system for a select purpose and do not actively browse the internet. <LineBreak/><LineBreak/>Note: Your system will be easier to hack and infect without security updates.</TextBlock>
<TextBlock Text=" " Margin="20,0,20,0" Padding="10" TextWrapping="WrapWithOverflow" MaxWidth="300"/> <TextBlock Text=" " Margin="20,0,20,0" Padding="10" TextWrapping="WrapWithOverflow" MaxWidth="300"/>
</StackPanel> </StackPanel>
</Border> </Border>