如何通过使用c#.net传递参数来设置动态线程的计时器 [英] how to set timer for dynamic thread by passing parameters by using c#.net
问题描述
我正在使用一个窗口服务应用程序,该应用程序具有动态线程生成的逻辑,该逻辑将用于为每个单独的属性ID从xml文件创建一个基于线程的单独数据提取,现在我要担心的是运行这些每个线程在不同的时间间隔内,一个线程每5分钟触发一次,另一个线程每天在触发模式,最后一个线程应该按月触发,请您能帮我一个忙吗?当我启动一个窗口服务时,所有线程都将启动,即使在工作完成后,这些生成的线程也不应消失,我们需要根据以上所述为这些线程设置一个计时器,我们该如何执行好的建议...
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; }
}
推荐答案
您的线程将运行,然后再次停止.已停止的线程无法再次启动.您需要某种类型的循环来保持线程运行.
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.
这是对您的代码的修改,其中我编写了一个类来在您的DoWork函数周围包裹这样的循环:
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
}
}
}
}
这篇关于如何通过使用c#.net传递参数来设置动态线程的计时器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!