Я пытаюсь сделать много асинхронных HttpWebRequest
.Это мой тестовый код:
class Program
{
static void Main(string[] args)
{
Test();
Console.ReadLine();
}
public static async void Test()
{
for (int i = 0; i < 10; i++)
{
int val = i;
await Task.Run(() => WR(val));
}
}
static async void WR(int msg)
{
Console.WriteLine(msg + " begin");
string url = "https://stackoverflow.com";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
var response = (HttpWebResponse)await Task.Factory.FromAsync<WebResponse>
(request.BeginGetResponse, request.EndGetResponse, null);
Console.WriteLine(msg + " status code: " + response.StatusCode);
Console.WriteLine(msg + " end");
}
}
Результат этого:
0 begin
1 begin
2 begin
3 begin
4 begin
5 begin
6 begin
7 begin
8 begin
9 begin
0 status code: OK
0 end
1 status code: OK
1 end
И после 1 end
ничего не происходит.Примерно через 30 секунд на выходе я вижу:
The thread 0x6634 has exited with code 0 (0x0).
The thread 0x5620 has exited with code 0 (0x0).
The thread 0x4d08 has exited with code 0 (0x0).
The thread 0x39b8 has exited with code 0 (0x0).
The thread 0x3454 has exited with code 0 (0x0).
The thread 0x99c has exited with code 0 (0x0).
The thread 0x6be0 has exited with code 0 (0x0).
Но нет никаких исключений, и консоль не закрывается.
Где моя ошибка?
ОТВЕТ:
Не помню response.Dispose();