.net - chaining array of tasks with continuation -
i have task structure little bit complex(for me @ least). structure is:
(where t = task)
t1, t2, t3... tn. there's array (a list of files), , t's represent tasks created each file. each t has 2 subtasks must complete or fail: tn.1, tn.2 - download , install. each download (tn.1) there 2 subtasks try, download 2 paths(tn.1.1, tn.1.2).
execution be:
first, download file: tn1.1. if tn.1.1 fails, tn.1.2 executes. if either download tasks returns ok - execute tn.2. if tn.2 executed or failed - go next tn.
i figured first thing do, write structure jagged arrays:
private void createtasks() { //main array task<int>[][][] maintask = new task<int>[_queuedapps.count][][]; (int = 0; < maintask.length; i++) { task<int>[][] arr = generateoperationtasks(); maintask[i] = arr; } } private task<int>[][] generateoperationtasks() { //two download tasks task<int>[] downloadtasks = new task<int>[2]; downloadtasks[0] = new task<int>(() => { return 0; }); downloadtasks[1] = new task<int>(() => { return 0; }); //one installation task task<int>[] installtask = new task<int>[1] { new task<int>(() => { return 0; }) }; //operations task jagged - keeps tasks above task<int>[][] operationtasks = new task<int>[2][]; operationtasks[0] = downloadtasks; operationtasks[1] = installtask; return operationtasks; }
so got maintask array of tasks, containing nicely ordered tasks described above. after reading docs on continuationtasks, realise not me since must call e.g. task.continuewith(task2). i'm stumped doing on maintask array. can't write maintask[0].continuewith(maintask[1]) because dont know size of array.
if somehow reference next task in array (but without knowing index), cant figure out how. ideas? thank help.
regards,
if want run operations in sequence on background thread, don't need lots of task
s, need one:
task.factory.startnew(downloadandinstallfiles)
and inside task
, can use normal sequential control flow constructs, foreach
, if
s want. like:
void downloadandinstallfiles() { foreach (var file in files) { var downloaded = download(file, primarysource); if (downloaded == null) downloaded = download(file, secondarysource); if (downloaded != null) install(downloaded); } }
Comments
Post a Comment