Search code examples
wpfbackgroundworkerpleasewait

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


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-experience 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


Solution

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