❔ Execute code on WPF program close
Hi, is there a way I can execute some code every single time the application closes. Either if it crashes, is closed manually, the computer restarts, or closed thru task manager or whatever?
For WPF
37 Replies
Like run it after it exits / crashes? i don't think so? You might have to write your own custom window service to monitor that.
if it crashes you could have a global exception handler
is its closed normally just handle the closed event
but if the process is killed im not sure you can do that
why do you even care about that
because my program creates a proxy
if you don’t shut it off correctly you don’t have any internet connection after the program closes
^
it would be the users fault that they killed the process
you shouldnt worry about that
well
still
i have to provide support if they suddenly don’t have internet anymore so its much easier to just do it programatically
it doesnt seem to be possible
i guess you could make it a windows service?
hm
okay
how do I do that? And what is it
Insteaf of making a windows service, I'm thinking of the program running a another small program thats hidden in the background, which checks periodically if the main program is still running, and if not, if it has shut down correctly, if so, the background program will shut down
I'd say making a Windows Service actually doesn't sound to bad
essentially, you want a small program running in the background that can detect when your application opens and closes, and swap the proxy setup as needed whenever that happens
yea
but I haven't worked with windows services before, and it seems like the user has to do smth when looking at the documentation?
I'm also gonna suggest that whatever you're doing, there's probably a more effective way to do it, that doesn't involve screwing with network config for the whole machine
Create Windows Service using BackgroundService - .NET
Learn how to create a Windows Service using the BackgroundService in .NET.
there isn't really
how so?
it has to monitor internet traffic so it uses a proxy at 127.0.0.1 to view it
you're trying to monitor all traffic for the whole machine?
yep
sounding more suspicious by the moment
lol
its nothing sus
i'm only looking for some specific requests
anyways, it doesn't really matter, is there some docs on how to make a windows service and run it from c#?, idk how they work, do I have to manually start and stop them or?
they can be automatic
but there isn't really a reason to have it running when my program isn't running
sure there is
?
it should only start when the program is opened, and close when the proxy is shut off
that's how you achieve the behavior you want
but for windows services, it seems like the user has to do a ton of stuff to make it run
not really
just has to install it
which requires admin privileges
the program already has admin privileges
which you need anyway to mess with network config
yea
but can I start the install from the program, or the user has to manually click a installer
¯\_(ツ)_/¯
you could probably make both happen
wdym
but can I start the install from the program, or the user has to manually click a installeryou could probably make both of these work
i guess the one from the program would be easiest
does your programm not already have its own installer?
no
sounds like it needs one
its a bundled .exe
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.