C
C#8mo ago
Ossie

❔ 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
BananaPie
BananaPie8mo ago
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.
sibber
sibber8mo ago
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
Ossie
Ossie8mo ago
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 ^
sibber
sibber8mo ago
it would be the users fault that they killed the process you shouldnt worry about that
Ossie
Ossie8mo ago
well still i have to provide support if they suddenly don’t have internet anymore so its much easier to just do it programatically
sibber
sibber8mo ago
it doesnt seem to be possible i guess you could make it a windows service?
Ossie
Ossie8mo ago
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
JakenVeina
JakenVeina8mo ago
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
Ossie
Ossie8mo ago
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?
JakenVeina
JakenVeina8mo ago
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
Ossie
Ossie8mo ago
there isn't really
JakenVeina
JakenVeina8mo ago
how so?
Ossie
Ossie8mo ago
it has to monitor internet traffic so it uses a proxy at 127.0.0.1 to view it
JakenVeina
JakenVeina8mo ago
you're trying to monitor all traffic for the whole machine?
Ossie
Ossie8mo ago
yep
JakenVeina
JakenVeina8mo ago
sounding more suspicious by the moment
Ossie
Ossie8mo ago
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?
JakenVeina
JakenVeina8mo ago
they can be automatic
Ossie
Ossie8mo ago
but there isn't really a reason to have it running when my program isn't running
JakenVeina
JakenVeina8mo ago
sure there is
Ossie
Ossie8mo ago
? it should only start when the program is opened, and close when the proxy is shut off
JakenVeina
JakenVeina8mo ago
that's how you achieve the behavior you want
Ossie
Ossie8mo ago
but for windows services, it seems like the user has to do a ton of stuff to make it run
JakenVeina
JakenVeina8mo ago
not really just has to install it which requires admin privileges
Ossie
Ossie8mo ago
the program already has admin privileges
JakenVeina
JakenVeina8mo ago
which you need anyway to mess with network config
Ossie
Ossie8mo ago
yea but can I start the install from the program, or the user has to manually click a installer
JakenVeina
JakenVeina8mo ago
¯\_(ツ)_/¯ you could probably make both happen
Ossie
Ossie8mo ago
wdym
JakenVeina
JakenVeina8mo ago
but can I start the install from the program, or the user has to manually click a installer
you could probably make both of these work
Ossie
Ossie8mo ago
i guess the one from the program would be easiest
JakenVeina
JakenVeina8mo ago
does your programm not already have its own installer?
Ossie
Ossie8mo ago
no
JakenVeina
JakenVeina8mo ago
sounds like it needs one
Ossie
Ossie8mo ago
its a bundled .exe
Accord
Accord8mo ago
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.