Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to traverse a tree (class containing parent and child properties of the same class) using threads?

How to traverse a tree (class containing parent and child properties of the same class) using threads?

Scheduled Pinned Locked Moved C#
cssdata-structurestutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rafferty Uy
    wrote on last edited by
    #1

    Hi, Having a class with parent child relationships is quite common (I think), and I normally traverse such trees using recursion. Class looks like this:

    public class Product {
    public List Parents {get; set;}
    public List Children {get; set;}
    public string Name {get; set;}
    public override string ToString() { return Name; }
    }

    Recursion will look like this:

    public void Dump(Product p) {
    Console.WriteLine(p.ToString());

    foreach (var child in p.Children)
        Dump(child);  // (1)
    

    }

    However, I was told by someone before that using threads will be faster and less expensive. Can anyone send me a sample code of how to do this? I'm thinking that I should simply change (1) to a ParameterizedThreadStart... but maybe there's a better practice? Thanks in advance.

    Rafferty

    M 1 Reply Last reply
    0
    • R Rafferty Uy

      Hi, Having a class with parent child relationships is quite common (I think), and I normally traverse such trees using recursion. Class looks like this:

      public class Product {
      public List Parents {get; set;}
      public List Children {get; set;}
      public string Name {get; set;}
      public override string ToString() { return Name; }
      }

      Recursion will look like this:

      public void Dump(Product p) {
      Console.WriteLine(p.ToString());

      foreach (var child in p.Children)
          Dump(child);  // (1)
      

      }

      However, I was told by someone before that using threads will be faster and less expensive. Can anyone send me a sample code of how to do this? I'm thinking that I should simply change (1) to a ParameterizedThreadStart... but maybe there's a better practice? Thanks in advance.

      Rafferty

      M Offline
      M Offline
      Migounette
      wrote on last edited by
      #2

      In fact, the person that told you this does not know the final step of your application or he tried to impress you :) You may have multiple threads in order to travers the tree, but in your case, the problem will be the output (Console.Writeline(....)) which is thread safe, you will have a lock in order to block multiple threads to access the output buffer. So, except if you are sure that you have a multiple core, in order millions of data (but in this case do not use the Console), I advise you to stay thread safe (1 thread and this one will catch the maximum of CPU) or otherwise you wil have a timce slice handling to do and lock/unlock to manage. You should have a good start with: Generic Background Worker[^] Good luck

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups