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

Did you find this page helpful?