Search code examples
c#.netwindowswinformstopmost

How do I create a WinForms application that locks/freezes every other application and can't be closed?


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


Solution

  • 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.