多线程代理检查器 [英] MultiThreaded Proxy Checker

查看:40
本文介绍了多线程代理检查器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些类似的代码:

            using (WebClient wc = new WebClient())
            {
                wc.Proxy = new WebProxy("IP", Port);

            resume:
                if (!wc.IsBusy)
                {
                    string rtn_msg = string.Empty;
                    try
                    {
                        rtn_msg = wc.DownloadString(new Uri("http://google.com/"));
                    }
                    catch (WebException) { }
                    catch (Exception) { }
                }
                else
                {
                    System.Threading.Thread.Sleep(1000);
                    goto resume;
                }
            }

我正在尝试将其与ThreadPool一起使用:

I am trying to use it with ThreadPool:

        foreach (Proxy proxy in s)
        {
            ThreadPool.QueueUserWorkItem((c) =>
            {
                this.CheckProxy(proxy);
            });
        }

问题是列表中的最后一个代理服务器已由所有线程检查.

The problem is that the last proxy in the list is checked by all threads.

例如,对于代理列表中的ip1,ip2,ip3,ip4,所有线程都检查ip4(列表中的最后一项).

For example, with ip1, ip2, ip3, ip4 in the proxy list, all the threads check ip4, the last item in list.

那是为什么?关于如何使它起作用的任何建议?

Why is that? Any suggestions on how I can get this to work?

推荐答案

如果您拥有ReSharper之类的工具,它会通过访问修改后的闭包来警告您.您需要制作本地副本:

If you had a tool like ReSharper it would have warned you with Access to modified closure. You need to make a local copy:

    foreach (Proxy proxy in s)
    {
        var p = proxy;
        ThreadPool.QueueUserWorkItem((c) =>
        {
            this.CheckProxy(p);
        });
    }

我也建议您将goto更改为while循环.Goto被认为是不好的做法,在您的情况下,您不会从中获得任何好处.

Also I would suggest to change your goto into a while loop. Goto is considered bad practice and in your case you do not gain anything from it.

这篇关于多线程代理检查器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆