在这里,我试图通过读取每个属性 id 的 xml 文档来生成动态线程,但是我面临一个问题,即如何将参数传递给动态线程,对于特定属性的哪些相关元素,有没有办法发送参数?请指教
在下面的线程中,我正在调用一个 dowork 方法,我必须传递特定属性 id 的元素的参数,我该怎么做?
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; }
}
如何调用在线程中采用多个参数的方法。我有一个方法叫
Send(string arg1, string arg2, string arg3)
这就是我向你们询问传递参数的任何解决方案的原因,注意这里我的线程方法是通过动态方式设计的,请问有什么建议吗?