-
March 8th, 2010, 09:43 AM
#1
Threadpool & Generic Lists
Hey all
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.
Help appreciated.
New to C# | Using VS 2008 with 3.5.
-
March 8th, 2010, 12:12 PM
#2
Re: Threadpool & Generic Lists
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|