如何通过使用3.5 c#.net将参数传递给线程 [英] how to pass parameters to thread by using 3.5 c#.net
问题描述
在这里,我试图通过读取每个属性ID的xml文档来生成动态线程,但是面临一个问题,即如何将参数传递给动态线程,对于该线程,特定属性的相关元素是否有任何发送方式参数?请指教
here i am trying to generate a dynamic threads by reading xml document for each attribute id,but am facing a problem that how to pass parameter to dynamic thread,for which related elements of the particular attribute, is there any way to send parameters? please advice
在下面的线程中,我正在调用dowork方法,我必须传递其特定属性ID的元素的参数,我该怎么做?
in below thread i am calling a dowork method, i have to pass parameters of elements of which particular attribute id how can i do that?
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; }
}
如何调用在一个线程中带有多个参数的方法.我有一个叫做
How do I call a method that takes multiple parameters in a thread. I have a method called
Send(string arg1, string arg2, string arg3)
那就是我要求你们提供传递参数的任何解决方案的原因,请注意这里我的线程方法是通过动态方式设计的,请提出任何建议吗?
thats the reason i have asked you guys for any solution to pass parameters,note here my threading methodology was designed by dynamical manner, any suggestion please ?
推荐答案
您已经传递了参数.我不确定我是否理解您的问题,但
You're already passing in parameters just fine. I'm not sure if I understand your question but
我必须传递其特定属性ID的元素的参数 我该怎么办?
i have to pass parameters of elements of which particular attribute id how can i do that?
听起来您需要的只是foreach
中的if statement
.您已经在遍历并创建线程/传递参数.您是否只是在问如何确保每个项目都有特定的属性ID?如果是这样-那就做到
It sounds like all you need is an if statement
inside your foreach
. You are already looping through and creating threads/passing params. Are you just asking how to make sure each item has a particular attribute id? If so - just make it
foreach (XElement host in xDoc.Descendants("Host"))
{
var hostID = (int) host.Attribute("id");
// Check ID here
if(hostID != ID_I_WANT)
continue;
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());
}
这篇关于如何通过使用3.5 c#.net将参数传递给线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!