J'ai une petite application MVVM qui communique avec une base de données. Quel (le cas échéant) est le moyen standard pour effectuer des transactions de base de données dans un thread d'arrière-plan qui met à jour l'interface utilisateur lorsque vous avez terminé? Dois-je utiliser BackgroundWorkers, TPL ou implémenter mes propres threads? Actuellement, j'ai une classe statique avec la méthode suivante pour le travail d'arrière-plan:Tâches, BackgroundWorkers ou nouveaux threads pour les transactions de base de données dans WPVM MVVM?
public static void RunAsync(Action backgroundWork, Action uiWork, Action<Exception> exceptionWork) {
var uiContext = TaskScheduler.FromCurrentSynchronizationContext();
// The time consuming work is run on a background thread.
var backgroundTask = new Task(() => backgroundWork());
// The UI work is run on the UI thread.
var uiTask = backgroundTask.ContinueWith(_ => { uiWork(); },
CancellationToken.None,
TaskContinuationOptions.OnlyOnRanToCompletion,
uiContext);
// Exceptions in the background task are handled on the UI thread.
var exceptionTask = backgroundTask.ContinueWith(t => { exceptionWork(t.Exception); },
CancellationToken.None,
TaskContinuationOptions.OnlyOnFaulted,
uiContext);
// Exceptions in the UI task are handled on on the UI thread.
var uiExceptionTask = uiTask.ContinueWith(t => { exceptionWork(t.Exception); },
CancellationToken.None,
TaskContinuationOptions.OnlyOnFaulted,
uiContext);
backgroundTask.Start();
}
Merci, c'est exactement ce que je cherchais! – Coder1095