Entity Framework 6 / SQL Server CE 4 SaveChangesAsync ()

I have the following method running on a non-GUI thread in my application:

private async Task PerformContextSubmitAsync()
{
    try
    {
        await DataContextProvider.GetDefaultContext().SaveChangesAsync();
    }
    catch (Exception ex)
    {
        Log.Error("Error performing context submit", ex);
    }
}

      

What is called like this:

await PerformContextSubmitAsync();

      

The app is a WPF / Prism based app, so I also post events to update the progress bar and the number of files in the GUI like:

_eventAggregator.GetEvent<DatabaseProgressEvent>().Publish(new DatabaseProgress(percentDone));

      

Everything works as expected, I can still see the "chunking" effect in UI updates that match the call SaveChangesAsync()

.

Basically it will block the thread that is running anyway. Is this a limitation of SQL Server CE or am I doing something really stupid?

I can find precious little about this google search and nothing on SO.

+3


source to share


1 answer


After some more testing, it turns out that I shouldn't use the keyword await

when calling the method async

.

Making a call like this:



Task.Run(() => PerformContextSubmitAsync());

      

Doesn't block the original calling thread, but it doesn't actually execute asynchronously. It seems more like postponing execution until a certain point in the future.

0


source







All Articles