C# .net For() Step?

.net, c#, for-loop, loops, multithreading

Solution

Do you really need to spawn a new thread for each work item? Unless there is a genuine need for this (if so, please tell us why), I would strongly recommend you use the Managed Thread Pool instead. This will give you the concurrency benefits you require, but without the resource requirements (as well as the creation, destruction and massive context-switching costs) of running thousands of threads. If you are on .NET 4.0, you might also want to consider using the Task Parallel Library.

For example:

for (var i = 0; i < ListProxies.Items.Count; i++)
{
   var s = ListProxies.Items[i] as string;
   ThreadPool.QueueUserWorkItem(ProxyTest.IsAlive, s);       
}

On another note, I would seriously consider renaming the `IsAlive` method (which looks like a boolean property or method) since:

- It clearly has a `void IsAlive(object)` signature.

- It has observable side-effects (from your comment that it "increment a progress bar and add a 'working' proxy to a new list").

Problem

I have a function, that processes a list of 6100 list items. The code used to work when the list was just 300 items. But instantly crashes with 6100. Is there a way I can loop through these 6100 items say 30 at a time and execute a new thread per item? ``` for (var i = 0; i < ListProxies.Items.Count; i++) { var s = ListProxies.Items[i] as string; var thread = new ParameterizedThreadStart(ProxyTest.IsAlive); var doIt = new Thread(thread) { Name = "CheckProxy# " + i }; doIt.Start(s); } ``` Any help would be greatly appreciated.

Original source