Friday, 15 March 2013

c# - Make IEnumerable method async -



c# - Make IEnumerable method async -

i have next node:

class node { public string name; public ienumerable<node> children; }

i have next extension method:

public static class extensionmethods { public static ienumerable<node> traversetree(this node root) { if (root.children != null) { foreach (var kid in root.children) { var nodes = traversetree(child); foreach (var node in nodes) { yield homecoming node; } } } yield homecoming root; } }

i want search node in tree name "foo". in order do:

node mynode = /* big tree! */ var search = mynode.traversetree().where(x=>x.name == "foo").firstordefault();

i have 3 goals

have method traversetree traverse tree yield (ienumerable) if 3th node happens have name == "foo" not have traverse entire tree. right case true. make method traversetree run on separate thread because may take long time find. hence guess traversetree method should take callback parameter? lastly nice able cancel operation. need pass method cancelation token well?

what right way of doing this?

sorry forgot mention using .net framework 4.0

this done code have. yay deferred execution. by far easier way of doing maintain method synchronous , move whole query thread. yes, cancellationtoken checked either traversal algorithm, kid selector, or both, add. other alternative have whatever that's waiting on result of traversal stop waiting, rather trying stop computation happening.

c# multithreading callback cancellation

No comments:

Post a Comment