0

嗨,我正在使用一个窗口服务应用程序,它具有动态线程生成的逻辑,它将用于创建一个基于单独线程的从 xml 文件中为每个单独的属性 id 获取数据,现在我担心的是我想在其中运行这些每个线程不同的时间间隔,一个线程每5分钟触发一次,另一个线程用于每天模式触发,最后一个线程应该按月触发,你能帮我看看我在想什么,当我启动一个窗口服务,所有要启动的线程,这些生成的线程即使暂时工作完成也不应该死掉,我们需要根据上面我说的为这些线程设置一个计时器,我们如何执行这个好心的建议...

下面我粘贴了相同的动态线程逻辑代码:

我想知道如何为每个不同的线程设置计时器

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

1 回答 1

0

您的线程将运行然后再次停止。已停止的线程无法再次启动。您需要某种类型的循环来保持线程运行。

这是对您的代码的修改,我在其中编写了一个类来围绕您的 DoWork 函数包装这样的循环:

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
            }
        }
    }
}
于 2012-06-13T15:44:59.703 回答