Question

I want the SaveChanges of the ObjectContext (Win apps) to save changes asynchronously, will show a marquee (or controllable?) progress bar (this I can easily implement) for the user while he is able to continue working.

I basically want to override the SaveChanges of the ObjectContext.

Has anyone thought about this before?

Was it helpful?

Solution

edit at 2013-03-12:

Since some people started to downvote this answer, I guess that, for some reason, this solution is not good anymore.

Please, don't downvote answers because of that. Always start with a gentle comment saying why you didn't like the answer.


I believe you need to use Asynchronous Delegates. It basically works like that:

  1. You create a delegate from the method you want to call asynchronously;

  2. You call BeginInvoke on the delegate, starting a call;

  3. You do whatever else you need to do (e.g. animate a marquee);

  4. You can either wait for the async call to finish, or check whether is has completed and keep animating the marquee if it isn't;

OTHER TIPS

Entity Framework itself currently does not support asynchronous operations. Mainly because it's built on top of ADO.NET where this isn't supported either. ADO.NET isn't even thread safe by default.

You can use the delegate approach above or wrap it into Task. But that will not use any async calls even if the provider supports it. Also during this "background" operation you should not do anything with the ObjectContext (querying, adding objects, ...) as may result in corrupted state.

Related to multithreading you can read this post. It's older, but ideas are still valid.

Edit 2013-04-17:

EF6 (the next version, currently in alpha stage in time of writing) will support asynchronous operations, namely your requested SaveChangesAsync. It also extended ADO.NET model, so if the provider itself supports asynchronous execution it will be really asynchronous (else back to former behavior as there's nothing better (wise) to do).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top