When performing a lengthy operation, I noticed that I can start the starting sub-operation from the beginning of the line and do other things while it is extracting the results from the caches / databases.
This operation:
public async Task<Fichaclis> Finalize()
{
using (TransactionScope transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
{
transactionTimer.Start();
var agendasTransitionTask = ExecuteAgendas();
... DO ALOT OF SYNC OPERATIONS ...
await agendasTransitionTask;
transaction.Complete();
}
}
private Task ExecuteAgendas()
{
return ags.GetAgendas().ContinueWith((prev) =>
{
var currentAgendas = prev.Result;
foreach (var item in currentAgendas)
{
... DO QUICK SYNC STUFF...
}
return ags.BulkEditAgenda(currentAgendas);
});
}
GetAgendas is a method used with the following signature:
public async Task<List<Agendas>> GetAgendas()
because it is widely used, I believe that the problem does not exist. Regarding BulkEditAgenda:
public async Task BulkEditAgenda(IEnumerable<Agendas> agendas)
{
if (agendas == null || agendas.Count() == 0)
{
return;
}
var t1 = AddOrUpdateCache(agendas);
var t2 = Task.Factory.StartNew(() =>
{
try
{
foreach (var item in agendas)
{
EditNoReconnection(item);
}
Save();
}
catch (Exception ex)
{
throw;
}
});
await Task.WhenAll(t1, t2);
}
EditNoReconnectand Save- both synchronization methods.
private Task AddOrUpdateCache(IEnumerable<Agendas> agendas)
{
var tasks = new List<Task>();
foreach (var item in agendas)
{
tasks.Add(TryGetCache(item)
.ContinueWith((taskResult) =>
{
...DO QUICK SYNC STUFF...
})
);
}
return Task.WhenAll(tasks);
}
TryGetCache also widely used, so I find it safe ... its signature private Task<AgendasCacheLookupResult> TryGetCache(
, : Finalize transaction.Complete() Save() ( BulkEditAgendas). , .
, Tasks, , Async/Await + Tasks/ContinueWith . ?