WebClient 不支持并发操作

今天试了一下,发现`WebClient`虽然支持异步,但果然不支持并发……

测试代码如下

Imports System.Net
Imports System.Threading

Module Module1

    Sub Main()
        Dim c As New WebClient
        Dim t1 = c.DownloadStringTaskAsync("http://www.baidu.com")
        Dim t2 = c.DownloadStringTaskAsync("http://www.zhihu.com")
        Thread.Sleep(10)
        Console.WriteLine(t1.IsCompleted)
        Console.WriteLine(t1.IsCompleted)
    End Sub

End Module

运行结果如下

未经处理的异常:  System.NotSupportedException: WebClient 不支持并发 I/O 操作。
   在 System.Net.WebClient.ClearWebClientState()
   在 System.Net.WebClient.DownloadStringAsync(Uri address, Object userToken)
   在 System.Net.WebClient.DownloadStringTaskAsync(Uri address)
   在 System.Net.WebClient.DownloadStringTaskAsync(String address)
   在 ConsoleApplication1.Module1.Main() 位置 ConsoleApplication1\Module1.vb:行号 9

测试环境:VS2013。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

ERROR: si-captcha.php plugin: GD image support not detected in PHP!

Contact your web host and ask them to enable GD image support for PHP.

ERROR: si-captcha.php plugin: imagepng function not detected in PHP!

Contact your web host and ask them to enable imagepng for PHP.

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

Content is available under CC BY-SA 3.0 unless otherwise noted.