C# get object from asyncron task -
i startet thread programming in c#. , realy dont know if right community ask questions that. if not, im sorry.
i have 2 functions, start() , status(). in start() create new task like:
start() { each(dir => listofalldirs) { new task(() => { yr.loadallfiles(dir); }).start(); } }
so have 200 parallel tasks running code:
class yr { list<values> rv = new list<values>(); public list<values> loadallfiles(string dir) { [...dostuff...] rv.add(...); rv.add(...); rv.add(...); } }
so dont know how access > 200 running threads data, before they're finished. i'm looking sth. like:
status(dir) { int count = gettaskbyname[dir].rv.count; (????) console.writeline("dir " + dir + "->" + count + "files checked"); }
and final output:
dir aa -> 19 files checkes dir ab -> 12 files checkes dir bb -> 49 files checkes dir aa -> 29 files checkes
so clear: 200 dirs, 200 tasks, running asyncron. how access specific task / data running task.
apparently, need take 200 directories , compute result each of them. plinq makes exceptionally easy:
var results = listofalldirs .asparallel() .select(dir => new { dir, result = yr.loadallfiles(dir) }) .tolist();
and there's list of directories plus computation result.
you can manually tasks well. in case want sequence this:
- create tasks , store them
- use task.waitall wait completion
- use task.result on each task result
Comments
Post a Comment