I've inherited a Winforms application that does a lot of long running calls into the application server from the UI thread, so the UI stays unresponsive, unusable, unclosable for quite some time. (Which makes me really go AAAAAAAAARGH!)
I plan to move the server calls to a background thread and have the UI disabled - but movable & closable - while the background thread does its work.
So what would be the best way to inhibit user input to my application? I'm thinking along the lines of "modal progress dialog", but I'd prefer a solution that does not force me to throw visuals into the face of the user (some server operations run within less than 500ms so a dialog is not optimal ...)
Is there any way in Winforms to prevent the user from launching actions or changing data in the application while letting through a few select things (resize, show, hide and family and the user closing the window)? I'd prefer a way that does not make me access every UI element in my forms and set it to disabled ... there are quite a lot of them and that application really has a "hacked in the UI designer until it shows flashy things" style of source code. No way of refactoring EVERY smelly thing until release date ...
Oh, by the way, this App lives in .net framework 2
The only way I know of is to get some/all controls as disabled. However, depending on how the controls are laid out, it isn't necessary to set every control as disabled - when a container control is disabled, then all of its children are disabled, too. For example, if you have a GroupBox
with some controls, if you set the GroupBox
to disabled, then all of the controls within the GroupBox
will be disabled, too.
Choose some upper level container control (might be the form itself), and set it's Enabled property to false. All controls placed on that control will be disabled, which will prevent them from receiving input.
The problem with simply enabling/disabling buttons is that UI events will still get added to the messagequeue while the task is running....
Say you have a Search button and a Reset button. When you click Search, you disable both buttons. The search runs for a while. If the user clicks Reset, even while it's disabled, the search will reset immediately upon completing.
The quick and dirty way around this is to add an
Application.DoEvents();
call immediately before re-enabling the buttons. DoEvents() will process all the click events on the controls, which will be ignored for controls that are still disabled.
On the Win32 API level, there's a EnableWindow function you can disable any window hand with (you need to get the underlying handle of your main window, of course, and of any other top level windows). I haven't tested it in WinForms, though. (I'm pretty sure there are other answers on the User32 level, but I can't remember the API calls right now.)
Note: this doesn't grey out controls, which decreases its utility from a usability point of view (the user just sees a 'frozen application'). It gets marginally better if you at least set an hourglass cursor.
However, this is a quick-and-dirty way of dealing with asynchronous processing. Do you really need to lock the user out of your entire application? Perhaps it's just a few functions that should be banned while a request is underway?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With