WPF Please Wait Using Background Workers - Is It Possible

Recently, our WPF / Entity Framework 4.0 application became unstable after we started using background workers to load data from an object. We did this to ensure that the "Please Wait'-spinner" graphic mode worked while the BG worker was extracting data from the database, but we began to experience numerous problems connecting to EF and other unexplained errors at random times. A message from a Microsoft EF team member showed that EF is not "thread safe", and as soon as we removed the working BG, the problems really disappeared.

This leaves us with a problem with the UI interface - we no longer have a convenient user "counter" that works when loading data. Does anyone have any suggestions regarding other ways in which this can be done without putting data loading into a BG worker?

thanks

+4
source share
2 answers

You can take an intermediate step. Instead of BackgroundWorker, create a single thread that will work until your application closes. This thread creates your connections and processes all communication with the database. Send messages to him, whether they do the work, and then get the results. It frees up the user interface while preventing multiple EF streams from being used simultaneously.

+5
source

I used to use BackgroundWorkers, and they are really designed to complete the task delegated to them in the background, create events related to the task, and die on completion. The most common example would be updating the progress bar from the background thread. User interface elements can only be updated from the user interface thread, so BackgroundWorkers that raise events in the user interface thread are desirable for this reason.

Having BackgroundWorker do things that require persistence, such as database connection operations, it looks like this will cause problems solely because of their temporary nature.

+1
source

All Articles