Which event fires when we cancel some App by Windows Task Manager? - c#

I need to know which event fires within an WPF application if I cancel it by Windows Task Manager?
The idea is to terminate internal App. job accurately.
Thank you!

When you use the "Processes" tab there is no event, because your process is simply killed.
When you use the "Applications" tab, a normal WM_CLOSE message is sent to the top level of your application. See the last answer here on how to detect this in a WPF application.

The idea is to terminate internal App. job accurately.
You can't do this reliably. If the user is ending the application that way, something has either gone badly wrong or they simply don't care. There's nothing you can do, you're toast. Don't worry about cleaning up after yourself: the operating system will do that for you, no thank you required.
The best thing that you can do is handle the standard close events. Those will get triggered if the user requests to end your app in a polite way, either via the normal means or through Task Manager (Task Manager will try to ask nicely first if the user clicks "End Task" from the "Applications" tab). But since I assume you're already doing that, you've done all that you can.

Handling the event from Task Manager is not possible as the way it works for ungracefull shut downs of applications.
However, you can try to handle the Application class' SessionEnding event which is described in MSDN at below link:
http://msdn.microsoft.com/en-us/library/system.windows.application.sessionending.aspx

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.

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.

Close Button on VS11 Apps

I try to create an Application on Windows 8 with VS 11 with C#, And I need to a Button in my App that when people Click on this button , my App going to Close.
But I can't find any Exit or Close function to add, to my Click Event.
how can I make this button? or what is your proposal to solving this Problem?
You shouldn't close your app.
This is one of the concepts of Metro apps.The user switches to a different task and the
Process Lifetime Management aspects kick in, first suspending your application and then later, if needed, terminating it.
Don't close the application's window: let the user know that there is no information currently available and then let them switch away, close, or wait as they'd prefer.
Took from here
If you don't care about certification - e.g. you want a close button in your own debug build to help you with testing - you can call Application.Current.Exit()
You must not implement Close Button.
If you do so, your app will be not able to sell in the store. See "Certification requirements for Windows apps".
3.6 Your app must use the mechanisms provided by the system for those features that have them
(snip)
Your app must neither programmatically close nor offer UI affordances to close it.
You want this?
App.Current.Exit();
Try this.. It worked
App.Current.Terminate();

Getting a formless C# program to close itself during shutdown/restart?

I'm writing a program which creates no forms at all until one is required. The problem is, it's preventing shutdown from continuing automatically. I've seen discussions about adding an if to form closing events to check if it's due to shutdown, but as I've said, my program is meant to have no forms at all until required.
Is there any event or some other method that will allow me to know when my program should be closing itself to allow for Windows to shut itself down automatically? And it's not multithreaded.
You can use the SystemEvents class, to "listen to" users logging out, or shutting down.
If I understand the documentation correctly (and a deep study with Reflector confirms this):
The systemevents will spawn a new thread that receives the messages from windows (it has its own messagepump).
When an event is received, your code will be called, from the new thread. You should be aware of this.
You could always add a dummy form which you open minimized, with no icon on the taskbar - it won't have any visual impact, but will be sent the form closing event - where you could note the shutdown event, and presumably shut down/stop whatever else there is that your application is doing.
Handling the Microsoft.Win32.SystemEvents.SessionEnding event, and checking if it's an actual shutdown with System.Environment.HasShutdownStarted

Handling a forced exit

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.

Categories

Resources