How can I dynamically name and instantiate List<string> lists?
-
I have a method that polls different containers of items that are varying in length. I'd like to divide the container item total count by pages with 1000 items per page, create a new list for each 1000 item page and then spawn a new thread for each page to process the 1000 items asynchronously. I'm not sure how to create a dynamically named list though? Thanks
-
I have a method that polls different containers of items that are varying in length. I'd like to divide the container item total count by pages with 1000 items per page, create a new list for each 1000 item page and then spawn a new thread for each page to process the 1000 items asynchronously. I'm not sure how to create a dynamically named list though? Thanks
I think to assist you with this we'd need to know some specifics about what "containers of items" are, and what type/structure/format data the results of the "polling" returns.
«I'm asked why doesn't C# implement feature X all the time. The answer's always the same: because no one ever designed, specified, implemented, tested, documented, shipped that feature. All six of those things are necessary to make a feature happen. They all cost huge amounts of time, effort and money.» Eric Lippert, Microsoft, 2009
-
I think to assist you with this we'd need to know some specifics about what "containers of items" are, and what type/structure/format data the results of the "polling" returns.
«I'm asked why doesn't C# implement feature X all the time. The answer's always the same: because no one ever designed, specified, implemented, tested, documented, shipped that feature. All six of those things are necessary to make a feature happen. They all cost huge amounts of time, effort and money.» Eric Lippert, Microsoft, 2009
Hi, They are active directory computer objects. I've typed them into strings from DirectoryServices.SearchResult to an all encompassing list and I'd like to divide them up from that point.
-
I have a method that polls different containers of items that are varying in length. I'd like to divide the container item total count by pages with 1000 items per page, create a new list for each 1000 item page and then spawn a new thread for each page to process the 1000 items asynchronously. I'm not sure how to create a dynamically named list though? Thanks
Why would you want to name them at all? Just dynamically create the list as usual, and pass it as a parameter to the thread. The tread constructor has an overload taking a ParameterizedThreadStart - MSDN[^] - allowing you to pass a single parameter as an object to the thread. Pass the newly created List, and cast it back in the thread.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
Hi, They are active directory computer objects. I've typed them into strings from DirectoryServices.SearchResult to an all encompassing list and I'd like to divide them up from that point.
What is stopping you at this point from creating a List<List<string>> with any arbitrary name and using that ?
private List> bunchOData = new List>();
private const int ChunkSize = 1000;private void MakeData()
{
List bigData = new List();for (int i = 0; i < 23678; i++) bigData.Add("x"); int limit = (bigData.Count / ChunkSize) \* ChunkSize; int finalChunk = bigData.Count - limit; for (int i = 0; i < limit; i += ChunkSize) { bunchOData.Add(bigData.GetRange(i, ChunkSize)); } bunchOData.Add(bigData.GetRange(limit, finalGulp));
}
While GetRange makes a shallow copy (references), the fact you are dealing with strings here makes me wonder about memory usage, particularly when you pass the List<string> items in 'bunchOData into separate threads. If you active-directory-whatever objects turned-into-strings are of varying lengths, then I don't think there's much you can do to avoid getting lots of string being created.
«I'm asked why doesn't C# implement feature X all the time. The answer's always the same: because no one ever designed, specified, implemented, tested, documented, shipped that feature. All six of those things are necessary to make a feature happen. They all cost huge amounts of time, effort and money.» Eric Lippert, Microsoft, 2009
-
What is stopping you at this point from creating a List<List<string>> with any arbitrary name and using that ?
private List> bunchOData = new List>();
private const int ChunkSize = 1000;private void MakeData()
{
List bigData = new List();for (int i = 0; i < 23678; i++) bigData.Add("x"); int limit = (bigData.Count / ChunkSize) \* ChunkSize; int finalChunk = bigData.Count - limit; for (int i = 0; i < limit; i += ChunkSize) { bunchOData.Add(bigData.GetRange(i, ChunkSize)); } bunchOData.Add(bigData.GetRange(limit, finalGulp));
}
While GetRange makes a shallow copy (references), the fact you are dealing with strings here makes me wonder about memory usage, particularly when you pass the List<string> items in 'bunchOData into separate threads. If you active-directory-whatever objects turned-into-strings are of varying lengths, then I don't think there's much you can do to avoid getting lots of string being created.
«I'm asked why doesn't C# implement feature X all the time. The answer's always the same: because no one ever designed, specified, implemented, tested, documented, shipped that feature. All six of those things are necessary to make a feature happen. They all cost huge amounts of time, effort and money.» Eric Lippert, Microsoft, 2009
I did not know you could do that, although I had always wondered how you could fit an array into an array. Thank you for that example, that worked well. After that bunchOData is populated, I'm trying to start a thread utilizing each 1000 page chunk of data, how can I pass the 1000 page chunk into a new thread? There will be multiple threads going with each 1000 page chunk with my loop and I'd like each thread to return a sorted list itself. Can I pass an unfiltered list to the thread and return a filtered list when the thread is done?
foreach (List<string> data in bunchOData )
{
var t = new Thread(() => searcher(computerNameArray, sidToSearchFor));
t.IsBackground = true;
t.Start();
}private List<string> searcher(List<string> computerNames, string sidToSearchFor)
{
List<string> returnValue = null;
//do stuff here
return returnValue;
} -
Why would you want to name them at all? Just dynamically create the list as usual, and pass it as a parameter to the thread. The tread constructor has an overload taking a ParameterizedThreadStart - MSDN[^] - allowing you to pass a single parameter as an object to the thread. Pass the newly created List, and cast it back in the thread.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
I believe I'm trying to do this very thing but having trouble passing an unfiltered list to the parameterized thread start function and then returning a filtered list from that parameterized thread start.
foreach (List<string> computerNameArray in computerNamesArray)
{
var t = new Thread(() => searcher(computerNameArray, sidToSearchFor));
t.IsBackground = true;
t.Start();
}private List<string> searcher(List<string> computerNames, string sidToSearchFor)
{
List<string> returnValue = null;
//do stuff here
return returnValue;
} -
I believe I'm trying to do this very thing but having trouble passing an unfiltered list to the parameterized thread start function and then returning a filtered list from that parameterized thread start.
foreach (List<string> computerNameArray in computerNamesArray)
{
var t = new Thread(() => searcher(computerNameArray, sidToSearchFor));
t.IsBackground = true;
t.Start();
}private List<string> searcher(List<string> computerNames, string sidToSearchFor)
{
List<string> returnValue = null;
//do stuff here
return returnValue;
}Passing a value into a Thread is pretty easy:
Thread t = new Thread(DoWork); t.Start("hello there"); } private void DoWork(object o) { string s = o as string; if (s != null) { Console.WriteLine(s); } }
Getting a return value is a lot harder! If I needed a return value, I'd probably do it via a BackgroundWorker instead of a Thread, because that makes it really easy:
BackgroundWorker work = new BackgroundWorker(); work.DoWork += work\_DoWork; work.RunWorkerCompleted += work\_RunWorkerCompleted; work.RunWorkerAsync("hello There"); } private void work\_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { Console.WriteLine(e.Result); } private void work\_DoWork(object sender, DoWorkEventArgs e) { string s = e.Argument as string; if (s != null) { Console.WriteLine(s); } e.Result = string.Format("{0}: Complete", e.Argument); }
Because Argument and Result are both objects, you can pass any type you like in and out.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...