views:

91

answers:

2

Hi,

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

+2  A: 

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.

John Fisher
We like this idea an will explore its possibilities for our next version. For now we are utilizing a simple Dispatcher.Invoke call that forces the UI to refresh with a 'loading' graphic just prior to our db call being made. Still, it would be ncie if microsoft gave EF some async functionality.
Gatmando
A: 

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.

Aphex