为什么WebClient会冻结我的程序?

时间:2017-07-25 16:02:02

标签: c# .net-3.5

我可以尝试从url获取字符串但是当我执行我的程序时GUI冻结

        private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (listBox1.Items.Count > 10)
            {
                MessageBox.Show("Wow ! You Try to hack iran ? :)");
                Application.Exit();
            }

            long fromNumber = Convert.ToInt64(textBox1.Text);
            long To = Convert.ToInt64(textBox2.Text);
            int badn = 0;
            int godn = 0;
            int ern = 0;
            string slas;
            long gethow = ((To - fromNumber) + 1);


            if (fromNumber < 9010000000 && To > 9399999999 && fromNumber > To)
            {
                MessageBox.Show("You entered wrong numbers !");
            }
            if (fromNumber >= 9010000000 && To >= 9010000000)
            {
                this.Text = "MyIrancell BruteForce  [ Status : Attacking ]";
                for (long i = 1; i < gethow; i++)
                {
                    int ico = Convert.ToInt32(i);
                    int ico2 = Convert.ToInt32(gethow + 1);
                    if (ico == ico2)
                    {
                        StartT.Stop();
                    }
                    string Irancell = "https://xxx.xxx/?id=";
                    // TODO: Complete member initialization
                    var request = Irancell + (fromNumber - 1 + i);
                    //
                     var client = new WebClient();
                     client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringCompletedHandler);
                     result=client.DownloadStringAsync(request);
                   //var result = client.DownloadString(request);
                    slas = "0" + Convert.ToString((fromNumber - 1 + i));
                    if (result == "2-P")
                    {
                        if (!listBox1.Items.Contains(slas))
                        {

                            listBox1.Items.Add(slas);
                            godn++;
                        }

                    }
                    if (result == "0")
                    {
                        badn++;
                        label12.Text = Convert.ToString(badn);


                    }
                    if (result != "0" && result != "2-P")
                    {
                        ern++;
                        label6.Text = Convert.ToString(ern);


                    }
                    label9.Text = Convert.ToString(listBox1.Items.Count);

                    //  if (i == gethow)
                    // {
                    //  groupBox1.Enabled = true;
                    //   groupBox2.Enabled = true;
                    //   button4.Enabled = true;

                    //}
                }
            }
        }

我在stackoverflow上读到了一些关于问题的消息,有人说使用client.DownloadStringAsync但我的program.i中的await是未知命令。编辑为完整代码

1 个答案:

答案 0 :(得分:3)

如果您在4.5之前定位.NET,则没有异步/等待 - 相反,您可以使用DownloadStringAsync的异步版本,在下载字符串时会引发事件

var client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringCompletedHandler);

for (long i = 1; i < gethow; i++)
{
    string WebAdress = "https://xxxxxx.xxx/web?id=";
    var request = WebAdress + (fromNumber - 1 + i);    

    client.DownloadStringAsync(request);

}

// -- > Elsewhere in your form
void DownloadStringCompletedHandler(object sender, DownloadStringCompletedEventArgs e)
{
    slas = "0" + Convert.ToString((fromNumber - 1 + i));
    if (e.Result == "Test")
    {
        if (!listBox1.Items.Contains(slas))
        {                
             listBox1.Items.Add(slas);
             godn++;
         }
    }
}

如果您的所有代码必须在您的按钮单击中,您可以内联事件处理程序

var client = new WebClient();
client.DownloadStringCompleted += (s,e) => {
    slas = "0" + Convert.ToString((fromNumber - 1 + i));
    if (e.Result == "Test")
    {
        if (!listBox1.Items.Contains(slas))
        {                
             listBox1.Items.Add(slas);
             godn++;
         }
    }
};

for (long i = 1; i < gethow; i++)
{
    string WebAdress = "https://xxxxxx.xxx/web?id=";
    var request = WebAdress + (fromNumber - 1 + i);    

    client.DownloadStringAsync(request);

}

编辑:您已将问题中的代码更新为此答案,但您假设DownloadStringAsync会返回结果 - 但事实并非如此。该方法什么都不返回。该字符串在事件处理程序

中返回
var client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringCompletedHandler);
result=client.DownloadStringAsync(request); // <-- This line is wrong

应该是

client.DownloadStringAsync(request);

并在DownloadStringCompletedHandler

中收到e.Result内的字符串
相关问题