mirror of
https://github.com/ChrisTitusTech/winutil.git
synced 2025-07-01 10:32:35 -05:00
Compare commits
1 Commits
Runspace-C
...
revert-270
Author | SHA1 | Date | |
---|---|---|---|
655324b927 |
10
Compile.ps1
10
Compile.ps1
@ -93,17 +93,11 @@ Get-ChildItem "config" | Where-Object {$psitem.extension -eq ".json"} | ForEach-
|
|||||||
$script_content.Add($(Write-output "`$sync.configs.$($psitem.BaseName) = '$json' `| convertfrom-json" ))
|
$script_content.Add($(Write-output "`$sync.configs.$($psitem.BaseName) = '$json' `| convertfrom-json" ))
|
||||||
}
|
}
|
||||||
|
|
||||||
# Read the entire XAML file as a single string, preserving line breaks
|
$xaml = (Get-Content "xaml\inputXML.xaml").replace("'","''")
|
||||||
$xaml = Get-Content "$workingdir\xaml\inputXML.xaml" -Raw
|
|
||||||
|
|
||||||
Update-Progress "Adding: Xaml " 90
|
Update-Progress "Adding: Xaml " 90
|
||||||
|
|
||||||
# Add the XAML content to $script_content using a here-string
|
$script_content.Add($(Write-output "`$inputXML = '$xaml'"))
|
||||||
$script_content.Add(@"
|
|
||||||
`$inputXML = @'
|
|
||||||
$xaml
|
|
||||||
'@
|
|
||||||
"@)
|
|
||||||
|
|
||||||
$script_content.Add($(Get-Content "scripts\main.ps1"))
|
$script_content.Add($(Get-Content "scripts\main.ps1"))
|
||||||
|
|
||||||
|
@ -20,52 +20,31 @@ function Invoke-WPFRunspace {
|
|||||||
|
|
||||||
[CmdletBinding()]
|
[CmdletBinding()]
|
||||||
Param (
|
Param (
|
||||||
[Parameter(Mandatory=$true)]
|
$ScriptBlock,
|
||||||
[scriptblock]$ScriptBlock,
|
$ArgumentList,
|
||||||
[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
|
||||||
$powershell.AddScript($ScriptBlock)
|
$script:powershell.AddScript($ScriptBlock)
|
||||||
if ($ArgumentList) {
|
foreach ($Argument in $ArgumentList) {
|
||||||
foreach ($Argument in $ArgumentList) {
|
$script:powershell.AddArgument($Argument)
|
||||||
$powershell.AddArgument($Argument)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$powershell.AddArgument($DebugPreference)
|
|
||||||
|
|
||||||
# Ensure runspace pool is available
|
|
||||||
if (-not $sync.runspace -or $sync.runspace.IsDisposed) {
|
|
||||||
throw "Runspace pool is not initialized or has been disposed."
|
|
||||||
}
|
|
||||||
$powershell.RunspacePool = $sync.runspace
|
|
||||||
|
|
||||||
# Execute the RunspacePool asynchronously
|
|
||||||
$handle = $powershell.BeginInvoke()
|
|
||||||
|
|
||||||
# Set up an event to handle completion
|
|
||||||
$null = Register-ObjectEvent -InputObject $powershell -EventName InvocationStateChanged -Action {
|
|
||||||
if ($EventArgs.InvocationStateInfo.State -eq "Completed") {
|
|
||||||
$powershell.EndInvoke($handle)
|
|
||||||
$powershell.Dispose()
|
|
||||||
[System.GC]::Collect()
|
|
||||||
Unregister-Event -SourceIdentifier $EventSubscriber.SourceIdentifier
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
# Return the handle
|
|
||||||
return $handle
|
|
||||||
}
|
}
|
||||||
catch {
|
$script:powershell.AddArgument($DebugPreference) # Pass DebugPreference to the script block
|
||||||
Write-Error "Error in Invoke-WPFRunspace: $_"
|
$script:powershell.RunspacePool = $sync.runspace
|
||||||
if ($powershell) { $powershell.Dispose() }
|
|
||||||
throw
|
# Execute the RunspacePool
|
||||||
|
$script:handle = $script:powershell.BeginInvoke()
|
||||||
|
|
||||||
|
# Clean up the RunspacePool threads when they are complete, and invoke the garbage collector to clean up the memory
|
||||||
|
if ($script:handle.IsCompleted) {
|
||||||
|
$script:powershell.EndInvoke($script:handle)
|
||||||
|
$script:powershell.Dispose()
|
||||||
|
$sync.runspace.Dispose()
|
||||||
|
$sync.runspace.Close()
|
||||||
|
[System.GC]::Collect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,66 +13,45 @@ 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 -not $DNSChange) {
|
if ($tweaks.count -eq 0 -and $sync["WPFchangedns"].text -eq "Default") {
|
||||||
$msg = "Please check the tweaks you wish to perform or select a DNS provider."
|
$msg = "Please check the tweaks you wish to perform."
|
||||||
[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)"
|
||||||
|
|
||||||
try {
|
Invoke-WPFRunspace -ArgumentList $Tweaks -DebugPreference $DebugPreference -ScriptBlock {
|
||||||
$handle = Invoke-WPFRunspace -ArgumentList $Tweaks, $DNSChange -DebugPreference $DebugPreference -ScriptBlock {
|
param($Tweaks, $DebugPreference)
|
||||||
param($Tweaks, $DNSChange, $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 0 -and $DNSChange) {
|
if ($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" })
|
||||||
} elseif ($Tweaks.count -eq 1) {
|
} else {
|
||||||
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" })
|
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Normal" -value 0.01 -overlay "logo" })
|
||||||
} else {
|
|
||||||
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Normal" -value 0.01 -overlay "logo" })
|
|
||||||
}
|
|
||||||
|
|
||||||
# Execute selected tweaks
|
|
||||||
for ($i = 0; $i -lt $Tweaks.Count; $i++) {
|
|
||||||
$currentTweak = $Tweaks[$i]
|
|
||||||
Set-WinUtilProgressBar -Label "Applying $currentTweak" -Percent ($i / $Tweaks.Count * 100)
|
|
||||||
|
|
||||||
# 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
|
|
||||||
$sync.ProcessRunning = $false
|
|
||||||
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "None" -overlay "checkmark" })
|
|
||||||
Write-Host "================================="
|
|
||||||
Write-Host "-- Tweaks are Finished ---"
|
|
||||||
Write-Host "================================="
|
|
||||||
}
|
}
|
||||||
|
# Execute other selected tweaks
|
||||||
|
|
||||||
# Optionally, you can add code here to update the UI or perform other tasks while the runspace is executing
|
for ($i = 0; $i -lt $Tweaks.Count; $i++) {
|
||||||
# For example, you might want to disable certain UI elements until the runspace completes
|
Set-WinUtilProgressBar -Label "Applying $($tweaks[$i])" -Percent ($i / $Tweaks.Count * 100)
|
||||||
|
Invoke-WinUtilTweaks $tweaks[$i]$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -value ($i/$Tweaks.Count) })
|
||||||
|
}
|
||||||
|
Set-WinUtilProgressBar -Label "Tweaks finished" -Percent 100
|
||||||
|
$sync.ProcessRunning = $false
|
||||||
|
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "None" -overlay "checkmark" })
|
||||||
|
Write-Host "================================="
|
||||||
|
Write-Host "-- Tweaks are Finished ---"
|
||||||
|
Write-Host "================================="
|
||||||
|
|
||||||
# If you need to wait for completion before proceeding, you can use:
|
# $ButtonType = [System.Windows.MessageBoxButton]::OK
|
||||||
# $handle.AsyncWaitHandle.WaitOne()
|
# $MessageboxTitle = "Tweaks are Finished "
|
||||||
# But be cautious about blocking the UI thread
|
# $Messageboxbody = ("Done")
|
||||||
|
# $MessageIcon = [System.Windows.MessageBoxImage]::Information
|
||||||
Write-Host "Tweaks execution started in background."
|
# [System.Windows.MessageBox]::Show($Messageboxbody, $MessageboxTitle, $ButtonType, $MessageIcon)
|
||||||
}
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user