C
C#13mo 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
BananaPie13mo 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
sibber13mo 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
OssieOP13mo 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
sibber13mo ago
it would be the users fault that they killed the process you shouldnt worry about that
Ossie
OssieOP13mo 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
sibber13mo ago
it doesnt seem to be possible i guess you could make it a windows service?
Ossie
OssieOP13mo 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
JakenVeina13mo 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
OssieOP13mo 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
JakenVeina13mo 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
OssieOP13mo ago
there isn't really
JakenVeina
JakenVeina13mo ago
how so?
Ossie
OssieOP13mo ago
it has to monitor internet traffic so it uses a proxy at 127.0.0.1 to view it
JakenVeina
JakenVeina13mo ago
you're trying to monitor all traffic for the whole machine?
Ossie
OssieOP13mo ago
yep
JakenVeina
JakenVeina13mo ago
sounding more suspicious by the moment
Ossie
OssieOP13mo 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
JakenVeina13mo ago
they can be automatic
Ossie
OssieOP13mo ago
but there isn't really a reason to have it running when my program isn't running
JakenVeina
JakenVeina13mo ago
sure there is
Ossie
OssieOP13mo ago
? it should only start when the program is opened, and close when the proxy is shut off
JakenVeina
JakenVeina13mo ago
that's how you achieve the behavior you want
Ossie
OssieOP13mo ago
but for windows services, it seems like the user has to do a ton of stuff to make it run
JakenVeina
JakenVeina13mo ago
not really just has to install it which requires admin privileges
Ossie
OssieOP13mo ago
the program already has admin privileges
JakenVeina
JakenVeina13mo ago
which you need anyway to mess with network config
Ossie
OssieOP13mo ago
yea but can I start the install from the program, or the user has to manually click a installer
JakenVeina
JakenVeina13mo ago
¯\_(ツ)_/¯ you could probably make both happen
Ossie
OssieOP13mo ago
wdym
JakenVeina
JakenVeina13mo 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
OssieOP13mo ago
i guess the one from the program would be easiest
JakenVeina
JakenVeina13mo ago
does your programm not already have its own installer?
Ossie
OssieOP13mo ago
no
JakenVeina
JakenVeina13mo ago
sounds like it needs one
Ossie
OssieOP13mo ago
its a bundled .exe
Accord
Accord13mo 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.
Want results from more Discord servers?
Add your server