Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to block Winforms UI while background thread is running

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

like image 828
froh42 Avatar asked Mar 15 '09 18:03

froh42


4 Answers

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.

like image 188
Andy Avatar answered Nov 15 '22 23:11

Andy


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.

like image 20
driis Avatar answered Nov 15 '22 22:11

driis


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.

like image 44
Handprint Avatar answered Nov 16 '22 00:11

Handprint


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?

like image 42
Pontus Gagge Avatar answered Nov 15 '22 22:11

Pontus Gagge