0

I am writing an application in c# to lock or freeze all programs untill user enters a value in the app's textbox and clicks ok.

The purpose of the app would be to get people to enter their time.

As far as I know you can set it to top most but they can end the app with task manager so am stuck here..

formName.TopMost = true;

Any help would be appreciated

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
James
  • 177
  • 10
  • 19
  • What OS are you aiming to run this on? When must it run? – ChrisBD Dec 21 '11 at 10:42
  • 3
    This is a pretty ancient topic: if you want all other programs to freeze, what if some other program wants to do the same thing? It's generally very bad to block everything out like that - who are you to say the user wasn't doing something incredibly important and time-sensitive? What if your app then hangs/crashes? The system is then unusable. – Kieren Johnstone Dec 21 '11 at 10:49
  • it is going to be used on windoes 7 os... well I dont care what they are doing or not or if it freezes their pc all i care about is to make them fill something – James Dec 21 '11 at 10:54
  • Wait, Raymond Chen must discuss that feature request somewhere... [Yeah, he did!](http://blogs.msdn.com/b/oldnewthing/archive/2005/06/07/426294.aspx) – sq33G Dec 21 '11 at 10:57
  • well it is for my work so the point is to fill timesheets by any means the app will be timesheet and as no one cares they will just end the app ....... – James Dec 21 '11 at 11:00
  • 1
    **Then just end their job or their pay.** This is not a programming problem, it's a human engineering problem. – Cody Gray - on strike Dec 21 '11 at 11:03

1 Answers1

2

Yes, that's correct. The Windows operating system allows multiple programs to run at one time. What you're experiencing is entirely by design.

If I remember correctly, the TopMost property applies only to windows in your process, and as you mention, it's all quite irrelevant: the user can still kill your application using the Task Manager.

There's no legitimate way of getting around that. It's not a "limitation", it's a feature. Any app that prevents itself from being closed by the Task Manager is treading dangerously closely on the category of software that we call malware. Nothing good can come out of pursuits like this.

Relevant reading: The arms race between programs and users


Perhaps a good compromise solution is to make your window/form actually top-most and disable the Close button so that the user knows they shouldn't try and close it. This is almost always enough to stop a user that is not determined to end your application by any means necessary, and that's about all you should ever be concerned with.

See the sample code here for how to make your window/form always appear on top of other running applications by setting the WS_EX_TOPMOST flag or toggling HWND_TOPMOST.

I've also already written a detailed answer here about disabling the Close button the correct way by setting the CS_NOCLOSE class style.

Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • What about in the case of software such as anti-virus? – Paul C Dec 21 '11 at 10:53
  • Thanks that is useful, but is it possible to just show them this and disable everything else or limit their use.. – James Dec 21 '11 at 10:55
  • @CodeBlend: What about an antivirus? I don't understand what that has to do with this. I've never run an antivirus program that took over my entire computer and prevented me from ending it. I hope I never do... – Cody Gray - on strike Dec 21 '11 at 11:04
  • @Samim: Did you read the top part of my answer? No, that's not possible. Your application only has control over *itself*. It can't control the rest of the computer. That's entirely by design. – Cody Gray - on strike Dec 21 '11 at 11:04
  • @CodyGray I like your answer but I was referring to the likes of antivirus or anti-cheat programs for that matter which if you try to the end them through task manager for example, will just start themselves again. – Paul C Dec 22 '11 at 15:36