Qualcuno può spiegare la differenza tra queste due affermazioni:Come posso produrre un task <Task> a scartare
Task<Task> bTask = backup.BackupCurrentDatabaseAsync()
.ContinueWith(_ => CompressArchiveAsync());
//unwrap the tasks to produce one entire task
Task t = bTask.Unwrap();
vs
Task<Task> bTask = backup.BackupCurrentDatabaseAsync()
.ContinueWith(_ =>
{
CompressArchiveAsync();
});
//unwrap the tasks to produce one entire task
Task t = bTask.Unwrap();
I metodi ExtractArchiveAsync()
, BackupCurrentDatabaseAsync()
, RestoreDatabaseAsync()
tutto restituiscono un Task
.
Qui, il primo Continuazione restituisce un Task<Task>
. Posso quindi eseguire l'attività Unwrap()
per continuare con l'attività risultante (interna).
La seconda versione non viene compilata. L'unica differenza qui sono le parentesi graffe attorno allo CompressArchiveAsync()
.
Sto tentando di accedere al risultante (interno) Task
per controllare lo Task.Status
. Se utilizzo il secondo metodo, Task.Status sta segnalando il risultato dell'attività BackupCurrentDatabaseAsync()
.
Nota a margine: prevedere il passaggio a 4.5 e async/awiat se possibile ... Produrrà codice più facile da leggere ... –
Purtroppo ancora sul 4! Come si raggiungerebbe quanto sopra in 4.5? – Simon
'async' /' await' è una funzionalità C# 5.0. Anche se funziona come previsto per .NET 4.5, puoi utilizzare il pacchetto di targeting asincrono per [portarlo su .NET 4.0] (http://stackoverflow.com/questions/9110472/using-async-await-on net-4). – CodesInChaos