开发者

Winforms-how do I keep window fully painted/responsive during a long running synchronous request is carried out?

开发者 https://www.devze.com 2022-12-23 16:35 出处:网络
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.

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 s开发者_JAVA百科etup 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


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?


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 ;).


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号