tags:

views:

39

answers:

3

Hi,

Background - For a winforms 3.5 c# application, I would like to keep the main window fully painted/responsive during a long running synchronous request. For example if the user moves the window.

Question - Is there a way to achive this WITHOUT having to setup a separate thread or backgroundworker process? (e.g. like a way to call from within my long running transaction, "release a bit of CPU to mainform", at some points)

thanks

+2  A: 

I'm sorry to say that the only way I know of to do what you ask is threaded processing. It's painful and has a world of hurdles all it's own, but I don't believe there's any other way.

On the good side, in this modern world the experience should prove quite useful for you ;).

Hardryv
+4  A: 

Synchronous means, by definition, that the call blocks until completion. If you block on the UI thread, then there is no way to keep the UI responsive at the same time. You can keep it painted, using the Control.Invalidate followed by Control.Update method, but it will still not be responsive to user input.

It's so easy to slap a BackgroundWorker on a form; why would you want to come up with clumsy workarounds anyway?

Aaronaught
+1. I agree completely!
David Stratton
I guess because it would seem to add unessessary complexity, and then I would have to handle how to disallow the user doing anything in the app whilst the task is running...
Greg
any advice re how to handle disallowing the user doing anything in the app whilst the task is running - perhaps there is a whole of form "disable all buttons" type method?
Greg
@Greg: If you want to disallow the user from doing *anything* then why do you need to keep the UI responsive? Are there specific actions you *do* want to allow? On most of my forms where I need to do background work, there are usually only 3-4 top-level controls to enable/disable.
Aaronaught
In any case, though, it's pretty easy to implement a "disable all controls" method if you want - you can iterate (and recurse) through all controls on the form and set `Enabled` to `false`. If some controls will already be disabled, then you can save those states in a dictionary for subsequent restore.
Aaronaught
Oh, one other thing... if you disable a `Panel`, `GroupBox`, `FlowLayoutPanel` or other layout control, it will disable all child controls as well. If you're not grouping your controls this way, start; it's not only easier to do things like this but it will make the UI a lot easier to program against in general.
Aaronaught
+2  A: 

There is not a way to do this without creating a new thread, either manually or via a BackgroundWorker. This is exactly why multi-threading is necessary.

Unless you're willing to consider a hack, such as having this long-running process be contained in a different executable, and have your main app call this executable... (but technically that exe would have its own thread. AND it's easier to use a background worker or just create a new thread.)

David Stratton