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
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
i have to pass parameters of elements of which particular attribute id how can i do that?
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());
}
Are you able to use TPL rather than starting your own threads?
If so you could just do this:
xDoc.Descendants("Host").AsParallel().ForAll(host =>
{
DoWork(new FileInfo
{
HostId = (int)xe.Attribute("id"),
Extension = (string)xe.Element("Extension"),
FolderPath = (string)xe.Element("FolderPath"),
});
});
Or did I miss the point of this question?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.