带检查的多线程

发布于 2024-11-03 12:35:46 字数 1899 浏览 0 评论 0原文

我想编写一个检查代理的程序...我想使用多线程,但不知道如何将其应用到我的程序中

int ktory = 0;

// Button to start multithreading
private void p_check_Click(object sender, EventArgs e)
{
    for (int i = 0; i < 10; i++)
    {
        Thread th = new Thread(test_proxy);
        CheckForIllegalCrossThreadCalls = false;
        th.Start();
    }

}

//This is my function to test proxies
private void test_proxy()
{
    try
    {

        int ile = p_listbox.Items.Count;

        string proxy = null;
        //'ktory' - means position in listbox
        proxy = p_listbox.Items[ktory].ToString();
        ktory += 1;

        //Splitting on IP and PORT
        int gdzie = proxy.IndexOf(":");
        string IP = proxy.Remove(gdzie);
        string ipp = proxy.Replace(IP + ":", "");
        int PORT = Int32.Parse(ipp);
        //end o splitting

        //My testing of anonimty ( works good don`t need to check)
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(p_proxyjudge.Text);
        WebProxy adr_proxy = new WebProxy(IP, PORT);
        adr_proxy.UseDefaultCredentials = true;
        request.Proxy = adr_proxy;
        request.Timeout = 15000;

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream stream = response.GetResponseStream();

        // Checking if anonymous ( not important)
        StreamReader sr = new StreamReader(stream);
        while (sr.EndOfStream != true)
        {
            string checking = sr.ReadLine();
            if (!checking.Contains("REMOTE_ADDR ="))
                p_work.Items.Add(proxy);
        }

        sr.Close();
        stream.Close();
    }
    catch (Exception ex)
    {
        ktory += 1;
    }
}

:编码: ile 和 ktory 就像 i 或 j 一样,是多个变量 >

但我的多线程停止在 10 个第一个代理上,或者只是检查它们 10 倍...

主要问题是让 10 个机器人检查代理,并在检查完一个代理后在列表框中移动到另一个代理(但仍有其他机器人在后台工作)

请帮助我:)我已经坐了第二天了,无法弄清楚

I wanted to write program that will check proxies... I wanted to use mutlithreading but don`t know really how to apply it to my program:

int ktory = 0;

// Button to start multithreading
private void p_check_Click(object sender, EventArgs e)
{
    for (int i = 0; i < 10; i++)
    {
        Thread th = new Thread(test_proxy);
        CheckForIllegalCrossThreadCalls = false;
        th.Start();
    }

}

//This is my function to test proxies
private void test_proxy()
{
    try
    {

        int ile = p_listbox.Items.Count;

        string proxy = null;
        //'ktory' - means position in listbox
        proxy = p_listbox.Items[ktory].ToString();
        ktory += 1;

        //Splitting on IP and PORT
        int gdzie = proxy.IndexOf(":");
        string IP = proxy.Remove(gdzie);
        string ipp = proxy.Replace(IP + ":", "");
        int PORT = Int32.Parse(ipp);
        //end o splitting

        //My testing of anonimty ( works good don`t need to check)
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(p_proxyjudge.Text);
        WebProxy adr_proxy = new WebProxy(IP, PORT);
        adr_proxy.UseDefaultCredentials = true;
        request.Proxy = adr_proxy;
        request.Timeout = 15000;

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream stream = response.GetResponseStream();

        // Checking if anonymous ( not important)
        StreamReader sr = new StreamReader(stream);
        while (sr.EndOfStream != true)
        {
            string checking = sr.ReadLine();
            if (!checking.Contains("REMOTE_ADDR ="))
                p_work.Items.Add(proxy);
        }

        sr.Close();
        stream.Close();
    }
    catch (Exception ex)
    {
        ktory += 1;
    }
}

< To code : ile and ktory are just like i, or j as numerous variable >

But my multithreading stops on 10 first proxies or just checks them 10x times as one...

The main problem is to make 10 bots to check proxies and after finished checking one proxy move onto another at listbox (but still others bots work in background)

PLEASE HELP ME :) I`m sitting 2nd day on it and cannot figure it out

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

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

发布评论

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

评论(1

断爱 2024-11-10 12:35:46

您没有锁定对 ktory 的访问,它正在被多个线程更改。因此,无法保证您的 10 个线程将检查 10 个不同的代理。

Object _lock = new Object();
int ktory=0;

...
private void test_proxy()
{
   try
   {
     int ile = p_listbox.Items.Count;

     string proxy = null;
     //'ktory' - means position in listbox
     lock (_lock) {
       proxy = p_listbox.Items[ktory].ToString();
       ktory += 1;
     }
...

You are not locking around access to ktory, which is being altered by multiple threads. Because of this, there is no guarantee that your 10 threads will check 10 different proxies.

Object _lock = new Object();
int ktory=0;

...
private void test_proxy()
{
   try
   {
     int ile = p_listbox.Items.Count;

     string proxy = null;
     //'ktory' - means position in listbox
     lock (_lock) {
       proxy = p_listbox.Items[ktory].ToString();
       ktory += 1;
     }
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文