Well I've got the basis of using single threads to process a piece of logic within my code, however I'm not certain about the threadpool. Below is a pseudo style example of what I'd like to achieve.
Code:
private List<string> ManyStrings = new List<string>();
private List<string> ProcessedStrings = new List<string>();
for (int i = 0; i < ManyStrings.Count; i++)
{
ThreadPool.QueueUserWorkItem(EditStrings);
}
static void EditStrings(object state)
{
// lots of string manipulation here
ProcessedStrings.Items.Add(newstring);
}
Obviously this code wouldn't work in reality, I think there needs to be some sort of logic to lock threads accessing the same string and add a new string (race condition). Also within the EditStrings method, external fuctions might be called, so the same may apply to this, and there needs to be a method of passing the a string into the EditStrings method, however it will only accept one parameter argument, so maybe using delegates would be nice.
The one parameter is of type object. All .NET types derive from System.Object, therefor, you can create a struct or a class which contains multiple properties/members. Construct said class, pass it in as the 'object' parameter to your thread. Then, within your thread method (ie: EditStrings), cast the parameter to the class type you created and extract the members from there.
Bookmarks