Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8057207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:57:10+00:00 2026-06-05T08:57:10+00:00

How can I generate a thread dynamically in an efficient and controlled manner? The

  • 0

How can I generate a thread dynamically in an efficient and controlled manner? The threads are to be created dynamically based on each XML host id.

Example:

samplethread1  for hostid:-1
samplethread2  for hostid:-2

Because I cannot rely on the host id count I need to make my code dynamic: suggest how I can have a control on each thread.

Given a piece of sample XML code:

<?xml version="1.0" standalone="yes"?>
  <HOSTS>
     <Host id = '1'>
       <Extension>txt</Extension>
       <FolderPath>C:/temp</FolderPath>
     </Host>
     <Host id = '2'>
       <Extension>rar</Extension>
       <FolderPath>C:/Sample</FolderPath>
     </Host>
  </HOSTS>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T08:57:12+00:00Added an answer on June 5, 2026 at 8:57 am

    I have to agree that the question is not particularly clear. But if you are looking to just create a new thread for every host, then how about this? This is using the .NET 4.0 Task Parallel Library. From .NET 4.0 onwards this is an easy way to harness the concurrent capabilities of your processor.

    static void Main(string[] args)
    {
        var currentDir = Directory.GetCurrentDirectory();
        var xDoc = XDocument.Load(string.Format(Path.Combine(currentDir, "Hosts.xml")));
        var taskFactory = new TaskFactory();
    
        foreach (XElement host in xDoc.Descendants("Host"))
        {
            var hostId = (int) host.Attribute("id");
            var extension = (string) host.Element("Extension");
            var folderPath = (string) host.Element("FolderPath");
            taskFactory.StartNew(() => DoWork(hostId, extension, folderPath));
        }
        //Carry on with your other work
    }
    
    static void DoWork(int hostId, string extension, string folderPath)
    {
        //Do stuff here
    }
    

    If you’re using .NET 3.5 or previous, then you can just create the threads yourself:

    static void Main(string[] args)
    {
        var currentDir = Directory.GetCurrentDirectory();
        var xDoc = XDocument.Load(string.Format(Path.Combine(currentDir, "Hosts.xml")));
        var threads = new List<Thread>();
    
        foreach (XElement host in xDoc.Descendants("Host"))
        {
            var hostID = (int) host.Attribute("id");
            var extension = (string) host.Element("Extension");
            var folderPath = (string) host.Element("FolderPath");
            var thread = new Thread(DoWork)
                             {
                                 Name = string.Format("samplethread{0}", hostID)
                             };
            thread.Start(new FileInfo
                             {
                                 HostId = hostID,
                                 Extension = extension,
                                 FolderPath = folderPath
                             });
            threads.Add(thread);
        }
        //Carry on with your other work, then wait for worker threads
        threads.ForEach(t => t.Join());
    }
    
    static void DoWork(object threadState)
    {
        var fileInfo = threadState as FileInfo;
        if (fileInfo != null)
        {
            //Do stuff here
        }
    }
    
    class FileInfo
    {
        public int HostId { get; set; }
        public string Extension { get; set; }
        public string FolderPath { get; set; }
    }
    

    This for me is still the best guide to Threading.

    EDIT

    So this is the task based version of what I think you’re getting at in your comment?

    static void Main()
    {
        var currentDir = Directory.GetCurrentDirectory();
        var xDoc = XDocument.Load(string.Format(Path.Combine(currentDir, "Hosts.xml")));
        var taskFactory = new TaskFactory();
    
        //I'm assuming this ID would normally be user input, or be passed from some other external source
        int hostId = 2;
    
        taskFactory.StartNew(() => DoWork(hostId, xDoc));
        //Carry on with your other work
    }
    
    static void DoWork(int hostId, XDocument hostDoc)
    {
        XElement foundHostElement = (from hostElement in hostDoc.Descendants("Host")
                                        where (int)hostElement.Attribute("id") == hostId
                                        select hostElement).First();
        var extension = (string)foundHostElement.Element("Extension");
        var folderPath = (string)foundHostElement.Element("FolderPath");
        //Do stuff here
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can generate my models and schema.yml file based on an existing database. But
ASP.NET MVC can generate HTML elements using HTML Helpers, for example @Html.ActionLink() , @Html.BeginForm()
I want to create an XSD which can generate the following XML. <note> <email:to>abc@def.com</email:to>
Example (works in Firefox): http://progamonth.com/files/tablestestfile.html I can't get select elements to dynamically get added
I have several threads running concurrently and each of them must generate random numbers.
here i am trying to generate a dynamic threads by reading xml document for
I know you can generate all permutations from a list, using glob or Algorithm::Permute
In a pure C++ world we can generate interfacing or glue code between different
I've heard that eclipse can generate accessor and mutator methods for you but can
I am aware that you can generate create scripts to generate database objects, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.