C# .net For() 步骤?

发布于 2024-10-06 16:45:56 字数 439 浏览 0 评论 0原文

我有一个函数,可以处理 6100 个列表项的列表。当列表只有 300 个项目时,该代码可以正常工作。但在 6100 时立即崩溃。有没有办法可以循环遍历这 6100 个项目(一次 30 个)并为每个项目执行一个新线程?

    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);
    }

任何帮助将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怀里藏娇 2024-10-13 16:45:56

真的需要为每个工作项生成一个新线程吗?除非确实需要这样做(如果是这样,请告诉我们原因),我强烈建议您使用 托管线程池 代替。这将为您提供所需的并发优势,但没有运行数千个线程的资源需求(以及创建、销毁和大量上下文切换成本)。如果您使用 .NET 4.0,您可能还需要考虑使用任务并行库

例如:

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

另一方面,我会认真考虑重命名 IsAlive 方法(看起来像布尔属性或方法),因为:

  1. 它显然有一个 void IsAlive(object)签名。
  2. 它具有明显的副作用(从您的评论来看,它“增加进度条并将“工作”代理添加到新列表中”)。

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:

  1. It clearly has a void IsAlive(object) signature.
  2. It has observable side-effects (from your comment that it "increment a progress bar and add a 'working' proxy to a new list").
鸠魁 2024-10-13 16:45:56

您可以生成的线程数量是有限制的。 6100个线程看起来确实有点太多了。

我同意 win Ani,您应该根据您想要完成的任务来研究 ThreadPool 甚至生产者/消费者进程。

有相当多的进程用于处理多线程应用程序,但如果不知道您一开始在做什么,除了线程池或生产者/消费者进程(带有 SyncEvents 的队列)之外,确实没有办法推荐任何方法。

无论如何,您确实应该尝试将线程数量保持在最低限度,否则您将面临线程锁、自旋锁、等待锁、死锁、竞争条件、谁知道会发生什么等风险……

如果您想要好的信息有关 C# 线程的信息,请参阅 Joe Duffy 所著的《Windows 上的并发编程》 书真的很有帮助。

There is a limit on the number of threads you can spawn. 6100 threads does seem quite a bit excesive.

I agree win Ani, you should look into a ThreadPool or even a Producer / Consumer process depending on what you are trying to accomplish.

There are quite a few processes for handling multi threaded applications but without knowing what you are doing in the start there really is no way to recommend any approach other than a ThreadPool or Producer / Consumer process (Queues with SyncEvents).

At any rate you really should try to keep the number of threads to a minimum otherwise you run the risk of thread locks, spin locks, wait locks, dead locks, race conditions, who knows what, etc...

If you want good information on threading with C# check out the book Concurrent Programming on Windows By Joe Duffy it is really helpful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文