开发者

WPF 'Please Wait' animation using background workers - is this possible

开发者 https://www.devze.com 2023-01-18 21:23 出处:网络
Recently our WPF/Entity Framework 4.0 application has become unstable after we began using backgroundworkers in order to load data from the entity. We were doing so in order to have a \'Please Wait\'-

Recently our WPF/Entity Framework 4.0 application has become unstable after we began using backgroundworkers in order to load data from the entity. We were doing so in order to have a 'Please Wait'-spinner graphic running while the BG worker retrieved data from the database, but we began experiencing numerous EF-related connection issues and other inexplicable errors at random times. A post from a Microsoft EF team member seemed to suggest that EF is not 'thread safe' and once we removed the BG workers the problems did indeed go away.

This leaves us with a UI-experie开发者_StackOverflow社区nce issue - we no longer have a nice user-friendly 'Please-Wait' spinner running while the data is loading. Does anyone out there have any suggestions as to other ways in which this can be done without placing the data-load into a BG worker?

Thanks


You can take an in-between step. Instead of a BackgroundWorker, create a single thread that runs until your app closes. That thread creates your connections and handles all database communication. Post messages to it, have it do the work, then receive the results. It will free up the UI, while preventing multiple threads from using EF simultaneously.


I've used BackgroundWorkers before and they're really meant for doing the task delegated to them in the background, raising events related to the task, and dying when finished. A most common example would be updating a progress bar from a background thread. UI elements can only be updated from the UI thread so BackgroundWorkers that raise events on the UI thread are desireable for this reason.

Having a BackgroundWorker do something that needs persistence, like operations over a database connection, seems like it would cause trouble purely because of their transient nature.

0

精彩评论

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

关注公众号