Hi All,
If I have a script that I build into an exe (no win forms), is there a way for me to preform an action if the user clicks on the X close button?
I ask because I'd like to perform a number of clean up actions.
Thanks
EXE - Exit (close) Actions
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.
- mxtrinidad
- Posts: 399
- Last visit: Tue May 16, 2023 6:52 am
Re: Post Exist Action
The executable self-contained. There's no "x" button in a executable, only in Windows Form.
Re: EXE - Exit (close) Actions
sorry, just to clarify, I mean this:
https://imgur.com/a/XywBbxJ
if this is still the windows form, then how would I call an action to take place if someone closes it?
thanks
https://imgur.com/a/XywBbxJ
if this is still the windows form, then how would I call an action to take place if someone closes it?
thanks
- mxtrinidad
- Posts: 399
- Last visit: Tue May 16, 2023 6:52 am
Re: EXE - Exit (close) Actions
Again, that's outside the executable and "x"-exit it's cause by a Windows mouse action.
The executable runs within the console and can't trap calls outside of the console.
The executable runs within the console and can't trap calls outside of the console.
- Alexander Riedel
- Posts: 8478
- Last visit: Tue Mar 26, 2024 8:52 am
- Been upvoted: 37 times
Re: EXE - Exit (close) Actions
In a console application there really is no option. In a Windows Application (there is a different host for that), which still is no form, we *could* add a close handler which gets called when the application is closed.
Not sure if that applies or is an option for what you are trying to do.
Not sure if that applies or is an option for what you are trying to do.
Alexander Riedel
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.
Re: EXE - Exit (close) Actions
Here is an example of how to trap the close events in a PowerShell console.
The code just sends a message when one of the termination control events is received (CTRL_*).
This can be modified to call a PowerShell function or scriptblock when the control message arrives.
Place in PS1 file and run with powershell. Ctl-C or the 'X' will trigger the handler.
This can be included as a snippet to be inserted into a script with PSS or PrimalScript.
Later I will work out the best way to add PS code to the handler.
Code: Select all
$code = @'
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Runtime.InteropServices;
namespace Win32API{
public class CloseHandler{
//static bool exitSystem = false;
[DllImport("Kernel32")]
private static extern bool SetConsoleCtrlHandler(EventHandler handler, bool add);
private delegate bool EventHandler(CtrlType sig);
static EventHandler _handler;
enum CtrlType{
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT = 1,
CTRL_CLOSE_EVENT = 2,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT = 6
}
private static bool Handler(CtrlType sig){
Console.WriteLine("Exiting system due to external CTRL-C, or process kill, or shutdown");
//do your cleanup here
Thread.Sleep(5000); //simulate some cleanup delay
Console.WriteLine("Cleanup complete");
//allow main to run off
//exitSystem = true;
//shutdown right away so there are no lingering threads
Environment.Exit(-1);
return true;
}
public void SetHandler(){
//exitSystem = true;
_handler += new EventHandler(Handler);
SetConsoleCtrlHandler(_handler, true);
}
}
}
'@
Add-Type $code
$handler = [Win32API.CloseHandler]::new()
$handler.SetHandler()
while(1){
Write-Host Sleeping...
sleep 2
}
This can be modified to call a PowerShell function or scriptblock when the control message arrives.
Place in PS1 file and run with powershell. Ctl-C or the 'X' will trigger the handler.
This can be included as a snippet to be inserted into a script with PSS or PrimalScript.
Later I will work out the best way to add PS code to the handler.
Re: EXE - Exit (close) Actions
Thank you for all the information guys!
I'll take a look at the example code from jvierra and see if I can work that into what I need to accomplish.
I'll take a look at the example code from jvierra and see if I can work that into what I need to accomplish.
Re: EXE - Exit (close) Actions
You will not do much because it only calls C# code. I posted it to show that the events can be caught and handled within a PowerShell console.
I am now trying to add an event generator to allow the code to generate an object event. This will allow the use of "Register-ObjectEvent" to cause the C# code to call arbitrary PowerShell code.
I haven't done this before mixing PS and C# classes with events. I suspect either Alex or David could do this in a few minutes. I have to sneak up on it the first time.
I will work on it tomorrow. I have all of the test code almost ready and now need to test and validate the behavior.
I am now trying to add an event generator to allow the code to generate an object event. This will allow the use of "Register-ObjectEvent" to cause the C# code to call arbitrary PowerShell code.
I haven't done this before mixing PS and C# classes with events. I suspect either Alex or David could do this in a few minutes. I have to sneak up on it the first time.
I will work on it tomorrow. I have all of the test code almost ready and now need to test and validate the behavior.
Re: EXE - Exit (close) Actions
Thanks again, jvierra.
I appreciate the efforts on putting something like this together.
I appreciate the efforts on putting something like this together.
Re: EXE - Exit (close) Actions
I discovered on issue. You cannot run any PowerShell code in this event or after it. Only simple C# code can be run. This is because the pipeline has already been terminated when the event is called. You can write to the console but you cannot read from it. You can use any basic C# code to log to a file or other duties but the PowerShell pipeline is gone so you cannot even inspect its variables.
This is a major limitation of all console based programs.
I can run the handler and trap the exit but no events can be received by PS once this is executed.
This is a major limitation of all console based programs.
I can run the handler and trap the exit but no events can be received by PS once this is executed.