nkat
Member
- Joined
- Jun 29, 2022
- Messages
- 21
- Programming Experience
- 1-3
Good day, everyone!
Here's this marvelous piece of code that uses Parallel.ForEach to build an Active Directory OU tree.
My aim is to figure out how it works, so I have rewritten the code without Parallel.ForEach and it works just fine, the idea is a simple recursion, alright.
Still, even though, I understand how the tree is built in no parallel fashion, I don't get how it is done in parallel. If I debug it, then the line #93
is just a line that does all the parallel magic and debugger cannot go in.
Would someone shed light on what's going on in there or recommend a way to "debug Tasks"
Here's this marvelous piece of code that uses Parallel.ForEach to build an Active Directory OU tree.
C#:
using System.DirectoryServices;
using System.Threading.Tasks;
public class ADTree
{
DirectoryEntry rootOU = null;
string rootDN = string.Empty;
List<ADTree> childOUs = new List<ADTree>();
public DirectoryEntry RootOU
{
get { return rootOU; }
set { rootOU = value; }
}
public string RootDN
{
get { return rootDN; }
set { rootDN = value; }
}
public List<ADTree> ChildOUs
{
get { return childOUs; }
set { childOUs = value; }
}
public ADTree(string dn)
{
RootOU = new DirectoryEntry(dn);
RootDN = dn;
BuildADTree().Wait();
}
public ADTree(DirectoryEntry root)
{
RootOU = root;
RootDN = root.Path;
BuildADTree().Wait();
}
private Task BuildADTree()
{
return Task.Factory.StartNew(() =>
{
object locker = new object();
Parallel.ForEach(RootOU.Children.Cast<DirectoryEntry>().AsEnumerable(), child =>
{
if (child.SchemaClassname.Equals("organizationalUnit"))
{
ADTree ChildTree = new ADTree(child);
lock (locker)
{
ChildOUs.Add(ChildTree);
}
}
});
});
}
}
My aim is to figure out how it works, so I have rewritten the code without Parallel.ForEach and it works just fine, the idea is a simple recursion, alright.
Still, even though, I understand how the tree is built in no parallel fashion, I don't get how it is done in parallel. If I debug it, then the line #93
C#:
return Task.Factory.StartNew(() => )
Would someone shed light on what's going on in there or recommend a way to "debug Tasks"