开发者

Lazy evaluation/initialization in GUI applications- Non-disruptive ways to do it?

开发者 https://www.devze.com 2023-02-19 15:26 出处:网络
Suppose you are making a GUI application, and you need to load/parse/calculate a bunch of t开发者_StackOverflow中文版hings before a user can use a certain tool, and you know what you have to do before

Suppose you are making a GUI application, and you need to load/parse/calculate a bunch of t开发者_StackOverflow中文版hings before a user can use a certain tool, and you know what you have to do beforehand. Suddenly, it makes sense to start doing these calculations in the background over a period of time (as opposed to "in one go" on start-up or exactly when it is needed). However, doing too much in the background will slow down the responsiveness of the application.

Are there any standard practices in this kind of approach? Perhaps ways to detect low load on the CPU or the user being idle and execute code in those times? Arguments against this type of approach?

Thanks!


Without knowing your app or your audience, I can't give you specific advice.

My main argument against the approach is that unless you have a high-profile application which will see a lot of use by non-programmers, I wouldn't bother. This sounds like a lot of busy work that could be spent developing or refining features that actually allow people to do new things with your app.

That being said, if there is a reason to do it, there is nothing wrong with lazy-loading data.

The problem with waiting until idle time is that some people have programs like SETI@Home installed on their computer, in which case their computer has little to no idle time. If loading full-throttle kills the responsiveness of your app, you could try injecting sleeps. This is what a lot of video games do when you specify a target frame rate, to avoid pegging the CPU. This would get the data loaded faster, rather than waiting for idle time.

If parts of your app depend on data to work, and the user invokes that part of the app, you will have to abandon the lazy-loading approach and resume your full CPU/disk taxing load. If it takes a long time, or make the app unresponsive, you could display a loading dialog with a progress bar.


If your target audience will tend to have a multicore CPU and if your app startup and background initialization tasks won't contend for the same other resources (e.g. disk IO, network, ...) to the point of creating a new bottleneck, you might want to kick off a background thread to perform the initialization tasks (or even a thread per initialization task if you have several tasks that can run in parallel). That will make more efficient use of a multicore hardware architecture.

You didn't specify your target platform, but it's exceedingly easy to achieve this in .NET and so I have begun doing it in many of my desktop apps.

0

精彩评论

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

关注公众号