Handling a forced exit - c#

Is there any good way to handle a forced exit in C#?
I have a formless C# application that talks to an LCD over serial. Once the application is running, the only way to kill it is with task manager. The trouble with this is that the program needs to turn the LCD off when it is done, and it doesn't look as if my Application.ApplicationExit event is ever fired in this condition.
Any ideas?

Once the application is running, the only way to kill it is with task manager.
My big idea would be to change this.
Stick an icon in the notification area that the user can use to shut your app down properly, or set it up so that running the app again will instead shut down an already-running instance if one exists, or any other way that sounds like a good idea.
Requiring a user to use Task Manager to shut down your application screams poor design.

Write a code in your program loop (with a timer perhaps) to read a file or a registry key. For example if a file at C:\YOURPROGRAM\CLOSEME contains text "closeme", close your program gracefully. Write another program that write that C:\YOURPROGRAM\CLOSEME file. So, whenever you want to shutdown your program, don't use taskmanager, instead, open second program.

Some options:
Write a separate process with a GUI that can start and stop the main process. For example, when you install the Apache web server on Windows the server itself is installed as a service. It can be started and stopped from the system services management panel, but it also comes with a "monitor" process that sits in the notification area, tells you whether Apache is running and lets you start or stop it manually.
If it's acceptable for your use-case, make the application a console application. You can register a handler for when the user presses CTRL+C (see Console.CancelKeyPress) that performs your cleanup before your process exits. This still won't let you handle someone killing the process from Task Manager, but it's very easy to do and might be good enough depending on your situation.

Related

Capture .Net Console Kill or Terminate

So I have a hidden console application called Hidden.exe running.
Another application Call Killer.exe will find the process Hidden.exe or its PID, and Kills the process.
How do i programmatically capture a kill command or a terminate from Task Manager? A user can browse through the process list and 'End Task' on Hidden.exe and I want to be able to capture this event and do some cleanup before it exits.
How can i do this? I have searched around, and explored alternatives from
.NET console application exit event
Send WM_CLOSE message to a process with no window
Can I send a ctrl-C (SIGINT) to an application on Windows?
etc....
But they all dont work or only work in some cases, my case is for a hidden console application and needs to somehow capture a Kill on it. None of the above solution seem to have a 'correct' solution.
There is no such answer. A kill will always work and will fire no event. This is due to security concern to prevent virus and/or malware code.
I've since found another way.

Until a process has started, do something

So I have a background program that starts with Windows, minimized to system tray icon. Once it loads I need it to constantly start checking if a process has started (for example VLC). Once the process has started, It must wait for it to close in order to start doing stuff and then get back to check if it has started. I've been trying to do this for a while now, but I just can't figure out how.
How would I constantly check if a program has started?
One way would be to have the Background Deamon look for aprogramm of a specific name. Unfortunately this not overly reliable (due to name overlaps), would require a lot of polling and runs the risk of race conditions (the process starting when your deamon is still working).
What would be reliable, is if it is the Deamon that actually starts the foreground process. That way it could do work before Process.Start() and after Process.WaitForExit(), with full information when both states happen: https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process?view=netcore-3.1
Steam is a good example. It is a single-instance process, so any further requests can be relayed to the running instance. The desktop links to programms/games are actually weblinks - not programm links. Those links use the :steam protocoll, wich is associated with the steam processes. So it goes like this:
user klicks on a WebLink with :steam procotoll
Windows resolves to hand this into a commandline call to the steam programm
A instance is started with the proper order in via commandline. Single instancing will not allow a 2nd instanc to start, but hand the request over a already running one
the already or now suddenly running instance calls the programm, having full data on when it starts and ends - being the actuall logical caller

c# windows shutdown control

Please read - this is not a duplicate.
I have a Windows service that runs under the LocalSystem acct. I am in need of restoring certain files prior to shutdown. I am able to intercept the Shutdown action at which point I initiate my restore process but the shutdown happens before I am able to complete.
I have enabled Shutdown privileges, added calls to AbortShutdown (Win32) and several other (ie shutdown /a) options but they do not work. I believe it is because the shutdown or restart has already been initiated and I have read that these functions only stop a shutdown while it is scheduled.
My goal is to intercept the shutdown, abort it, complete my tasks and then shutdown. I just need more time and the amount of time is dependent on the number and size of files I have to restore.
I am open to any suggestion and appreciate your time and consideration.
Thanks!
This sounds more like something that can be done with windows batch files.
Your goal is untenable. On Windows, the user has control over the shutdown process. If the user wants to shutdown the machine, it's not up to your software to contravene the user's intentions.
The burden is on you to make sure that your software can deal with a shutdown, whenever it happens. This usually means making sure that file operations are as atomic as possible, or using a "hot journal" file to recover (on the next system startup) in the event that atomic operations can't be completed.
The files that you speak of, that you have to restore before shutdown, should be "restored" (hard to tell exactly what you mean here) as soon as any operations are finished on them. Without further details, it hard to discuss the specific approaches.

Control console for C# .net program

I'm running a C# .net program under mono on Unix and I'm looking to control or change the behavior of the program after it has started.
I'll write the new functions into the program so I just need to trigger them without restarting the program.
I'm thinking I could allow the program to accept messages such as SOAP but feel this might be insure. It might be better if I could control the program locally from a separate program but i'm unsure of where to start.
Is there a way to give instructions into the program after it has started without a separate program or if the separate program is the solution does anyone know where to start with this?
Thanks!
I think you can create some UI form into your app and you will be able to controll your app by using this UI. If it isn't using you can hide that form to tray.
Or you can use second app and send some messages to your first application by using sockets, for example.
The way this is usually done in Unix programs (reference) is by sending the SIGHUP signal, and letting the program interpret this as a command to reload its configuration file.
Sending a signal can be done from a terminal or script with the Unix kill command (which is named this way because the default signal is SIGTERM to request the process to shut down itself).
This is how you send SIGHUP to a process with a certain PID:
kill -HUP [pid]
You can use the mono UnixSignal class to handle these Unix signals in a .NET program. One way is to use .WaitOne to wait for the signal on a dedicated thread. Another way is by regularly polling .IsSet or .Count.

Launch Process from Windows Service

I am having one windows forms application which is designed to do specific tasks in background. Now I want to make sure that this application should be running all the time.
No one should able to close it. If some one closed it from Task Manager (Kill it) then it should restart it self.
I had couple of options for that. I have tried to make one window service which has timer and which can be check at every 1 minute that if process is not found then it will launch the process. But I have gone through couple of articles and they are saying that this is not nice idea. Is there any other way round to keeping alive my application in windows.
In my idea also if someone closes my service then also I can't detect if my WinForms application is closed or running.
What is best way to do so? I am ready to give highest privileges and I have thought that option as well that If someone kill process of my application then computer should be shut down it self.
Please share better idea to do so.
If you don't want the user closing your app, make it as difficult for him as possible:
launch it maximized
remove the frame of the window (and close, maximize,minimize buttons with it)
launch it TopMost
set ShowInTaskbar of your forms to false
ask 10 times "are you sure you want to exit???" :)
set e.Cancel to true in FormClosing event, etc...
About the Task Manager:
I think you can disable task manager altogether http://www.techulator.com/resources/3480-how-disable-task-manager-windows.aspx
Or you can hide the process (ugly and virus-like): How do I hide a process in Task Manager in C#?
Or you can sort-of make it harder to kill the process: Making an app/service such that trying to end/kill its process in Task Manager would result in "Unable to Terminate Process"
Then, if the user still manages to close your app, you can do what most people on the Internet consider a Very Bad Idea and start it from the service. As long as you are concious of the risks.
There are plenty of resource out there that tell you how to start an interactive app from the service (so evidently some people are doing it too), for example:
http://blogs.msdn.com/b/winsdk/archive/2009/07/14/launching-an-interactive-process-from-windows-service-in-windows-vista-and-later.aspx
you should know that it is impossible to prevent task manager not to close your application. and its not a proper idea to force the user.
But, if you insist I think the best way is through services with timer or thread whichever suits your solution to check its process and run it if not exists. and you didn't mention in your post that what was the reason of not using this method and why it is not a good method.
hope it helps you decide better.

Categories

Resources