M
M
Mikhail2018-02-17 19:03:17
C++ / C#
Mikhail, 2018-02-17 19:03:17

Why is the second thread "blocked" when trying to http-request?

Hello. There is a method for sending requests:

public static XDocument _request(string Url)
{
     WebRequest Request = WebRequest.Create(Url);
     Stream XmlStream = Request.GetResponse().GetResponseStream();
     return XDocument.Load(XmlStream);
}

I'm trying to run two threads:
foreach (string Link in Links)
{
    TimerCallback RSSCallback = new TimerCallback(ReadRSS);
    Timer Timer = new Timer(Test, Link, 0, 3000);
}

public static void Test(object obj)
{ 
    string Url = (string)obj;
    Console.WriteLine($"Запрос на {Url}");
    RSS._request(Url);
}

As a result, I see the following:
5a8851eca4c69116617098.png
That is, for some reason, requests are processed only in one thread, everything is quiet in the other. Empirically, I found out that this does not depend on the URL in any way. That is, if you change the elements in the Links array, then only the first thread still works.
Where is the mistake?
UPD. Empirically, it was revealed that the error in the line return XDocument.Load(XmlStream). If you remove it, then all requests are normally sent to both resources without any problems.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Yudakov, 2018-02-18
@AlexanderYudakov

I do not really understand why all these Timer and TimerCallback.
If you just need to run several queries in parallel, I suggest this code:

TimerCallback RSSCallback = new TimerCallback(ReadRSS);
Timer Timer = new Timer(Test, Link, 0, 3000);

replace with this:
new Task(Test, Link).Start();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question