As promised, here is a standard windows forms app packaged as a 64 bit V5 exe.
Run it on your machine, close the form and see if the process exits.
There is no special code to make it exit the process as you can see in the included .psf and .psbuild files.
EXE process does not end on exit
Forum rules
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Do not post any licensing information in this forum.
Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
- Alexander Riedel
- Posts: 8478
- Last visit: Tue Mar 26, 2024 8:52 am
- Been upvoted: 37 times
Re: EXE process does not end on exit
- Attachments
-
- processtest.zip
- (135.75 KiB) Downloaded 177 times
Alexander Riedel
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.
Re: EXE process does not end on exit
Thank you for processtest. It worked as advertised. Tested on Win7 x64 and Win10.
I'm unfamiliar with the config and manifest. Would these be necessary when distributing the exe?
I built the EXE again using a default embedded manifest and a config.
Same results.
During execution of the EXE I can see the java runspaces kick off, each perform its duty, generate a log and close during the life cycle of each runspace.
I left out the GUI. The scriptblock is stripped down. The rest is verbatim.
I'm unfamiliar with the config and manifest. Would these be necessary when distributing the exe?
I built the EXE again using a default embedded manifest and a config.
Same results.
During execution of the EXE I can see the java runspaces kick off, each perform its duty, generate a log and close during the life cycle of each runspace.
I left out the GUI. The scriptblock is stripped down. The rest is verbatim.
- #region Synchronized Collections
- $runspace = [hashtable]::Synchronized(@{})
- $syncHash = [hashtable]::Synchronized(@{})
- $jobCleanup = [hashtable]::Synchronized(@{})
- $jobs = [collections.arraylist]::Synchronized([collections.arraylist]@())
- $collection = [hashtable]::Synchronized(@{})
- #endregion Synchronized Collections
- #region Extra things for Sync'ed Collections
- $collection.ObservableCollection = New-Object System.Collections.ObjectModel.ObservableCollection[object]
- $jobCleanup.Flag = $True
- #endregion Extra things for Sync'ed Collections
- #region UI Runspace Creation
- $runspace.Runspace =[runspacefactory]::CreateRunspace()
- $runspace.Runspace.ApartmentState = "STA"
- $runspace.Runspace.Open()
- $runspace.Runspace.SessionStateProxy.SetVariable("runspace",$runspace)
- $runspace.Runspace.SessionStateProxy.SetVariable("syncHash",$syncHash)
- $runspace.Runspace.SessionStateProxy.SetVariable("jobCleanup",$jobCleanup)
- $runspace.Runspace.SessionStateProxy.SetVariable("jobs",$jobs)
- $runspace.Runspace.SessionStateProxy.SetVariable("collection",$collection)
- $runspace.PowerShell = [powershell]::Create().AddScript({
- $syncHash.Error = $Error
- #GUI Section
- #region Background runspace to clean up jobs
- $newRunspace =[runspacefactory]::CreateRunspace()
- $newRunspace.ApartmentState = "STA"
- $newRunspace.Open()
- $newRunspace.SessionStateProxy.SetVariable("jobCleanup",$jobCleanup)
- $newRunspace.SessionStateProxy.SetVariable("jobs",$jobs)
- $newRunspace.SessionStateProxy.SetVariable("syncHash",$syncHash)
- $jobCleanup.PowerShell = [powershell]::Create().AddScript({
- #Routine to handle completed runspaces
- Do {
- [System.Threading.Monitor]::Enter($Jobs.syncroot)
- Foreach($runspace in $jobs)
- {
- If ($runspace.Handle.isCompleted)
- {
- $null = $runspace.powershell.EndInvoke($runspace.Handle)
- $runspace.powershell.dispose()
- $null = $runspace.Handle
- $null = $runspace.powershell
- }
- }
- #Clean out unused runspace jobs
- $temphash = $jobs.clone()
- $temphash | Where {
- $_.runspace -eq $Null
- } | ForEach {
- $jobs.remove($_)
- }
- [System.Threading.Monitor]::Exit($Jobs.syncroot)
- Start-Sleep -Seconds 1
- } while ($jobCleanup.Flag)
- })
- $jobCleanup.PowerShell.Runspace = $newRunspace
- $jobCleanup.Thread = $jobCleanup.PowerShell.BeginInvoke()
- #endregion
- $syncHash.Verify.Add_Click({
- $ps = [powershell]::Create().AddScript({
- Param ($Collection, $syncHash, $jobs, $Data )
- $RunspacePool = [runspacefactory]::CreateRunspacePool(1,5)
- $ScriptBlock{
- Param()
- Java.exe -jar C:\Verify\Verify.jar $Row arg2
- }
- Foreach ($Row in $Rows)
- {
- $ps = [powershell]::Create()
- $ps.RunspacePool = $RunspacePool
- $RunspacePool.Open()
- $null = $ps.AddScript($ScriptBlock).AddArgument($Row).AddArgument($syncHash)
- $null = $jobs.Add([pscustomobject]@{
- PowerShell = $Ps
- Handle = $PS.BeginInvoke()
- })
- }
- }).AddArgument($Collection).AddArgument($syncHash).AddArgument($jobs).AddArgument($Row)
- [System.Threading.Monitor]::Enter($Jobs.syncroot)
- $jobs.Add([pscustomobject]@{
- PowerShell = $ps
- Handle = $Ps.BeginInvoke()
- })
- [System.Threading.Monitor]::Exit($Jobs.syncroot)
- })
- $syncHash.Window.Add_Closed({
- #Halt job processing
- $jobCleanup.Flag = $False
- #Stop all runspaces
- $jobCleanup.PowerShell.Dispose()
- $runspace.PowerShell.Runspace.Close()
- $runspace.PowerShell.Dispose()
- [gc]::Collect()
- [gc]::WaitForPendingFinalizers()
- [System.Windows.Forms.Application]::Exit();Stop-Process -Name "CustomApp" -Force
- })
- #endregion UI Events
- $syncHash.Window.ShowDialog()
- })
- $runspace.PowerShell.Runspace = $runspace.Runspace
- $runspace.Handle = $runspace.PowerShell.BeginInvoke()
- #endregion UI Runspace Creation
Re: EXE process does not end on exit
Please post your file as an attachment. It needs to be the complete PS1 file you are running as you run it and it must cause the issue.
Note how Alex posted his ZIP file.
Note how Alex posted his ZIP file.
Re: EXE process does not end on exit
The code you posted has syntax errors. The code must be posted as a file and must be runnable as it is posted.
A quick look tells me that you are likely deadlocking on a synchash. Without some clear runnable code there is no real way to sort through some hundreds of lines to see hat you are doing.
I will also recommend converting your code to use a workflow which is a controlled and PowerShell compatible way to use multiple runspaces/threads. It is more efficient, easier to manage and easier to code than runspaces.
A quick look tells me that you are likely deadlocking on a synchash. Without some clear runnable code there is no real way to sort through some hundreds of lines to see hat you are doing.
I will also recommend converting your code to use a workflow which is a controlled and PowerShell compatible way to use multiple runspaces/threads. It is more efficient, easier to manage and easier to code than runspaces.
Re: EXE process does not end on exit
Here is a quick example of how a workflow does all of the work you are trying to do:
Open a new copy of PS command line version and paste the above code. Look at how the workflow generates reusable runspaces. They are left for reuse. When PowerShell closes it will clean these up without any intervention.
There is no need to use synchashes as PS manages access to variables for you in a safe way. A workflow can be set to control the number of tasks running in parallel and as each finishes the runspace will be used to launch another queued task.
Code: Select all
Get-RunSpace
workflow Test-Workflow{
$Disks = Get-Disk
ForEach -Parallel ($Disk in $Disks){
# The commands run sequentially on each disk.
$Disk.Path
}
}
Test-Workflow
Get-Runspace
There is no need to use synchashes as PS manages access to variables for you in a safe way. A workflow can be set to control the number of tasks running in parallel and as each finishes the runspace will be used to launch another queued task.
Re: EXE process does not end on exit
Is there an inherent issue if I am using WPF instead of winforms?
I have never used winforms, I learned on WPF.
Thank you.
I have never used winforms, I learned on WPF.
Thank you.
Re: EXE process does not end on exit
To build the EXE I am using Powershell Studio 2017.
The primary reason I managed to get a budget for the software was the argument I could convert editable scripts into a locked down exe.
The primary reason I managed to get a budget for the software was the argument I could convert editable scripts into a locked down exe.
Re: EXE process does not end on exit
Here is a sanitized version of the script with the main elements in place.
This would be enough to launch the script but not do anything, which is fine because I can launch the exe successfully.
I can close the window using the button in the upper right but that's when the process does not end.
Thank you The build settings are on the previous page of this post.
This would be enough to launch the script but not do anything, which is fine because I can launch the exe successfully.
I can close the window using the button in the upper right but that's when the process does not end.
Thank you The build settings are on the previous page of this post.