我尝试制作许多 async 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).
但是没有任何例外,并且控制台没有关闭。
我的错误在哪里?
喵喵时光机
qq_花开花谢_0
动漫人物
相关分类