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.
Related
Long story short, I need to create an application that monitor the sound volume on a computer. The computer's user must not be able to stop the application no matter what.
I'll need to make my app start on computer start up, so the user can't just restart it to enter his session without the application running. As of now I don't know precisely how to do it but with some research this shouldn't be a problem.
My biggest concern is if he just stops the process in the task manager. I guess that I can't avoid that programmatically ? Is there a way to just modify the session's right so that it can't stop processes in the task manager? Or any other solution I didn't think about ?
Or there is no way I can do that and I'll just have to trust my user not to ever stop the program in the task manager ?
Thanks in advance for your help. :)
PS : This will run on a computer in a student club that runs the music for the club and that anyone can access. There are chambers where people sleep the floors above so we don't want people to put the volume too loud. That's it. No malware or anything.
In general, the only programs that act like that are malicious (e.g. rootkits). If you think about it, you really wouldn't want programs to be able to act like that.
As others have indicated, the closest you'll get here is a Windows Service, which automatically starts with Windows. Average users won't know to stop it, but it's still possible to stop it manually for power users.
One work-around you could try is to periodically have it call a web service to verify that it's running. That way you could tell who might have uninstalled or stopped the service. (The problem, of course, is that they might just not have their computer on or something; you could have separate calls for "Start" and "Still On").
Alternatively, if this installed only on computers that are exclusively under your direct control, as others have indicated, you could configure things so that you need administrative access to stop the process. This option was addressed in the comments.
I'm not accusing you of malware - just wanted to illustrate how bad it would be if you could easily make a program that the user can "never stop".
So no you can't make a program that a computer-savvy person could never stop.
But from what I gathered....
Sounds like you want to make it a service. https://msdn.microsoft.com/en-us/library/d56de412(v=vs.110).aspx.
Another (easier I think) option is just to make a console app that starts up from the Task Scheduler http://www.c-sharpcorner.com/uploadfile/manas1/console-application-using-windows-scheduler/.
Long story short, I need to create an application that monitor the sound volume on a computer. The computer's user must not be able to stop the application no matter what.
I'll need to make my app start on computer start up, so the user can't just restart it to enter his session without the application running. As of now I don't know precisely how to do it but with some research this shouldn't be a problem.
My biggest concern is if he just stops the process in the task manager. I guess that I can't avoid that programmatically ? Is there a way to just modify the session's right so that it can't stop processes in the task manager? Or any other solution I didn't think about ?
Or there is no way I can do that and I'll just have to trust my user not to ever stop the program in the task manager ?
Thanks in advance for your help. :)
PS : This will run on a computer in a student club that runs the music for the club and that anyone can access. There are chambers where people sleep the floors above so we don't want people to put the volume too loud. That's it. No malware or anything.
In general, the only programs that act like that are malicious (e.g. rootkits). If you think about it, you really wouldn't want programs to be able to act like that.
As others have indicated, the closest you'll get here is a Windows Service, which automatically starts with Windows. Average users won't know to stop it, but it's still possible to stop it manually for power users.
One work-around you could try is to periodically have it call a web service to verify that it's running. That way you could tell who might have uninstalled or stopped the service. (The problem, of course, is that they might just not have their computer on or something; you could have separate calls for "Start" and "Still On").
Alternatively, if this installed only on computers that are exclusively under your direct control, as others have indicated, you could configure things so that you need administrative access to stop the process. This option was addressed in the comments.
I'm not accusing you of malware - just wanted to illustrate how bad it would be if you could easily make a program that the user can "never stop".
So no you can't make a program that a computer-savvy person could never stop.
But from what I gathered....
Sounds like you want to make it a service. https://msdn.microsoft.com/en-us/library/d56de412(v=vs.110).aspx.
Another (easier I think) option is just to make a console app that starts up from the Task Scheduler http://www.c-sharpcorner.com/uploadfile/manas1/console-application-using-windows-scheduler/.
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();
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.
I have a windows form application which needs to be the TopMost. I've set my form to be the TopMost and my application works as I'd like it to except for in one case.
There is a 3rd party application (referred to as player.exe) that displays SWF movie files on a portion of the screen that popup on top of my application.
Using Process Monitor I determined that player.exe application calls
flash.exe <PositionX> <PositionY> <Width> <Height> <MovieFile>
in my case:
flash.exe 901 96 379 261 somemovie.swf
Since flash.exe is being spawned in a new process after my form has been set to the TopMost it is appearing on top of my application.
First thing I did was make my application minimize the player.exe main application window hoping that this would prevent the Flash from appearing also. But, unfortunately it doesn't... even with the window minimized whenever the flash movie starts it shows up at the pixel location (901,96). I then tried creating a timer to keep setting the form.TopMost property to true every 10ms. This sort of works but you still see a very quick blip of the swf file.
Is there some type of Windows API call which can be used to temporarily prevent player.exe from spawning child processes which are visible? I admit it sounds a little far fetched. But, curious if anyone else has had a similar problem.
Addendum:
This addendum is to provide a reply to some of the suggestions layed out in Mathew's post below.
For the emergency situation described in the comments, I would look at possible solutions along these lines:
1) How does the third party application normally get started and
stopped? Am I permitted to close it
the same way? If it is a service, the
Service Control Manager can stop it.
If it is a regular application,
sending an escape keystroke (with
SendInput() perhaps) or WM_CLOSE
message to its main window may work.
Easiest way to close the app is to CTRL-ALT-DEL, then kill process. -OR-
The proper way is to Hold ESC while clicking the left mouse button... then input your username and password, navigate some menu's to stop the player.
There is no PAUSE command... believe it or not.
I don't think using WM_CLOSE will help since minimizing the application doesn't. Would that kill the process also? If not, how do you reopen it.
2) If I can't close it nicely, am I permitted to kill it? If so,
TerminateProcess() should work.
I can't kill the process for two reasons. 1) Upon relaunch you need to supply username/password credentials... There may be a way to get around this since it doesn't prompt when the machine is rebooted but... 2) Whenever I kill the process in task manager it doesn't die gracefully and asks if you want to send an error report.
3) If I absolutely have to leave the other process running, I would try
to see if I can programmatically
invoke fast user switching to take me
to a different session (in which there
will be no competing topmost windows).
I don't know where in the API to start
with this one. (Peter Ruderman
suggests SwitchDesktop() for this
purpose in his answer.)
I got really excited by this idea... I found this article on CodeProject which provides a lot of the API Wrapper methods. I stopped implementing it because I think that in order for desktop's to work you must have explorer.exe running (which I do not).
EDIT2: On second thought... maybe explorer.exe isn't needed. I'll give it a try and report back.
Edit3: Was unable to get the code in that article working. Will have to put this on hold for a moment.
Answer Summary
As one might have expected, there is no simple answer to this problem. The best solution would be to problematically switch to a different desktop when you need to guarantee nothing will appear over it. I was unable to find a simple C# implementation of desktop switching that worked and I had a looming doubt that I would just be opening a whole new set of worms once it was implemented. Therefore, I decided not to implement the desktop switching. I did find a C++ Implementation that works well. Please post working C# virtual desktop implementations for others.
Setting the TopMost property (or adding the WS_EX_TOPMOST style to a window) does not make it unique in the system. Any number of topmost windows may be created by any number of applications; the only guarantee is that all topmost windows will be drawn 'above' all non-topmost windows. If there are two or more topmost windows, the Z-order still applies. From your description, I suspect that flash.exe is also creating a topmost window.
Aside from periodically forcing your window to the top of the Z-order, I think there is little you can do. Be warned, however, that this approach is dangerous: if two or more windows are simultaneously trying to force themselves to the top of the Z-order, the result will be a flickering mess that the user will likely have to use the task manager to escape.
I recommend that your program not attempt to meddle with other processes on the computer (unless that is its explicit purpose, e.g. a task manager clone). The computer belongs to the user, and he may not value your program more highly than all others.
Addendum:
For the emergency situation described in the comments, I would look at possible solutions along these lines:
How does the third party application normally get started and stopped? Am I permitted to close it the same way? If it is a service, the Service Control Manager can stop it. If it is a regular application, sending an escape keystroke (with SendInput() perhaps) or WM_CLOSE message to its main window may work.
If I can't close it nicely, am I permitted to kill it? If so, TerminateProcess() should work.
If I absolutely have to leave the other process running, I would try to see if I can programmatically invoke fast user switching to take me to a different session (in which there will be no competing topmost windows). I don't know where in the API to start with this one. (Peter Ruderman suggests SwitchDesktop() for this purpose in his answer.)
You can use the Process class to start flash.exe directly - and use an appropriate ProcessStartInfo settings to show the window in a hidden state - or with a WindowStyle of hidden or minimized.
You could also consider using the SetWindowsHookEx API to intercept the process start API calls, and when the process is flash.exe run some code to restore you window to top-most status.
Matthew's answer is excellent, but I suspect you may be asking the wrong question. Why does your application need to be topmost? If you're trying to create a kiosk or some such, then topmost is not the way to go.
Edit: After reading your response to Matthew's comment, I'd suggest creating a new desktop and switching to it before displaying your alert. (See CreateDesktop and SwitchDesktop in MSDN.)