0

こんにちは、私は、個々の属性 ID ごとに xml ファイルから個別のスレッド ベースのフェッチ データを作成するために使用する動的スレッド生成のロジックを持つウィンドウ サービス アプリケーションを使用しています。さまざまな時間間隔、1 つのスレッドを使用して 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 に答える