多线程

时间:2016-06-26 09:52:00

标签: c# httpwebrequest webrequest

所以我有一个程序可以发送许多webrequests。它像这样发送:

be.AsParallel().WithDegreeOfParallelism(1).ForAll(Send);

这将发送它并将其添加到ThreadPool

private void Send(string emailName)
{
        ServicePointManager.DefaultConnectionLimit = int.MaxValue;
        ServicePointManager.Expect100Continue = false;
        Console.Title =
            $"{emailName}{_domain} /::/ {_tries} /::/ {Sw.Elapsed.ToString("dd\\:hh\\:mm\\:ss")}";
        _tries++;

        ThreadPool.QueueUserWorkItem((o) =>
        {
            var request =
                (HttpWebRequest)
                    WebRequest.Create("http://www.realmofthemadgod.com/account/forgotPassword?guid=" + emailName +
                                      _domain);
            request.Proxy = null;
            request.Method = "GET";
            request.Timeout = 60000;
            request.KeepAlive = false;

            var response = (HttpWebResponse) request.GetResponse();

            // ReSharper disable once AssignNullToNotNullAttribute
            using (var reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII))
            {
                if (reader.ReadToEnd() != "<Success/>") return;

                if (logToFile)
                {
                    lock (_syncRoot)
                    {
                        var reader1 = File.ReadAllText(@"C:\Users\" + _userPath + @"\Desktop\accounts.txt");
                        using (var writer = File.AppendText(@"C:\Users\" + _userPath + @"\Desktop\accounts.txt")
                            )
                        {
                            if (!reader1.Contains(emailName + _domain))
                                writer.WriteLine(emailName + _domain);
                            writer.Close();
                        }
                    }
                }

                Console.WriteLine($"Found: " + emailName + _domain);
                _synthesizer.Speak("New account found");
            }
        });
    }

如果请求返回&#39;成功/&#39;它基本上会告诉我。

当我让它运行时,它会在大约0.5秒内完成700个请求,但之后需要几分钟才能告诉我结果。 我想知道如何制作它,以便在发送请求时它会告诉我它是否返回&#39;成功/&#39;而不是5分钟后。

0 个答案:

没有答案