how to set timer for dynamic thread by passing par

2019-08-16 15:11发布

问题:

hi i am using a window service application which was having a logic of dynamic threading generation which will use to create a separate thread based fetching data from the xml file for each individual attribute id, now my concern is i want to run those each threads in the different time intervals, one thread use to fire for every 5 min , another thread use to fire for every day mode, last thread should to be fire by monthly manner, can you please help me is it possible what i am thinking, when i start up a window service, all the thread to be start , these generated thread should not die even for the time being work was completed, we need set up a timer for those threads based on above i said,how can we perform that kindly advice...

here below i have pasted the same code of dynamic threading logic:

i want to know how set timer for each different thread

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; }
}

回答1:

Your thread would run and then stop again. A thread that has stopped cannot be started again. you need some type of loop to keep the thread running.

Here is a modification of your code where i wrote a class to wrap such a loop around you DoWork function:

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Linq;
using System.Threading;

namespace App
{
    class FileInfo
    {
        public int HostId { get; set; }
        public string Extension { get; set; }
        public string FolderPath { get; set; }
    }

    class ThreadSet
    {
        public ThreadStart action;
        public FileInfo file;
        public string name;
        public Boolean shouldrun = true;
        public Thread thread;

        public ThreadSet(XElement host, Action<Object> threadaction)
        {
            int hostID = (int)host.Attribute("id");
            name = string.Format("samplethread{0}", hostID);
            file = new FileInfo
                {
                    HostId = hostID,
                    Extension = (string)host.Element("Extension"),
                    FolderPath = (string)host.Element("FolderPath")
                };
            action = () =>
            {
                while (shouldrun)
                {
                    threadaction(file);
                }
            };
            thread = new Thread(action);
            thread.Name = name;
        }

        public void Start()
        {
            thread.Start();
        }

        public void Join()
        {
            shouldrun = false;
            thread.Join(5000);
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            var currentDir = Directory.GetCurrentDirectory();
            var xDoc = XDocument.Load(string.Format(Path.Combine(currentDir, "Hosts.xml")));
            var threads = new List<ThreadSet>();

            foreach (XElement host in xDoc.Descendants("Host"))
            {
                ThreadSet set = new ThreadSet(host, DoWork);
                set.Start();
                threads.Add(set);
            }
            //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
            }
        }
    }
}