Parallel.For运行100次然后停止?

时间:2018-04-28 18:53:20

标签: c# .net

我试图浏览列表,但我的迭代只会在停止之前运行100次。我实际上并行运行了6次,所以它应该运行超过100次,特别是我将循环设置为100k。

for (int i = 0; i < 100000; i++)内的代码似乎只在所有并行线程中运行100次。队列有超过100万个项目,因此队列不是问题所在。

public class ProxyWorker
{
    private static int _proxyCount;
    private static ConcurrentQueue<long> _idsToConvert;
    private static List<Proxy> _proxies;

    public ProxyWorker(int proxyCount, ConcurrentQueue<long> idsToConvert, List<Proxy> proxies)
    {
        _proxyCount = proxyCount;
        _idsToConvert = idsToConvert;
        _proxies = proxies;
    }

    public void Run()
    {
        Parallel.For(0, _proxyCount, index =>
        {
            ConvertOnProxy(index);
        });
    }

    static void ConvertOnProxy(int proxyId)
    {
        using (var webClient = new WebClient())
        {
            var proxy = _proxies[proxyId];
            webClient.Proxy = new WebProxy(proxy.Host, proxy.Port);

            for (int i = 0; i < 100000; i++)
            {
                if (_idsToConvert.IsEmpty)
                {
                    Logger.Success("Finished processing");
                    return;
                }
                else if (!_idsToConvert.TryDequeue(out var currentId))
                {
                    Logger.Error("Failed to process userID: " + currentId);
                }
                else
                {
                    try
                    {
                        ProcessId(currentId);
                    }
                    catch (WebException e)
                    {
                        try { ProcessId(currentId); } catch { Logger.Error(e.Message); }
                    }
                }
            }
        }
    }

    static async void ProcessId(long currentId)
    {
        var username = await TwitterUtilities.GetUsernameFromId(currentId);

        Program.UsernamesProcessed.Add(username);
        Program.IdsProcessed.Add(currentId);

        if (username == "no_username_found")
        {
            return;
        }

        Program.UsernameCache.Add(username);
        Program.IdCache.Add(currentId);
    }
}

0 个答案:

没有答案