开发者

Multithreading with checking

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 ano开发者_JAVA百科nymous ( 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


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;
     }
...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜