Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Dispatchermer和WebClient.DownloadStringAsync throw“;WebClient不支持并发I/O操作”;例外_C#_Multithreading_Concurrency_Dispatchertimer - Fatal编程技术网

C# Dispatchermer和WebClient.DownloadStringAsync throw“;WebClient不支持并发I/O操作”;例外

C# Dispatchermer和WebClient.DownloadStringAsync throw“;WebClient不支持并发I/O操作”;例外,c#,multithreading,concurrency,dispatchertimer,C#,Multithreading,Concurrency,Dispatchertimer,我需要这个代码的帮助 WebClient client = new WebClient(); string url = "http://someUrl.com" DispatcherTimer timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromSeconds(Convert.ToDouble(18.0)); timer.Start();

我需要这个代码的帮助

WebClient client = new WebClient();
    string url = "http://someUrl.com"

    DispatcherTimer timer = new DispatcherTimer();
                timer.Interval = TimeSpan.FromSeconds(Convert.ToDouble(18.0));
                timer.Start();

                timer.Tick += new EventHandler(delegate(object p, EventArgs a)
                {
                     client.DownloadStringAsync(new Uri(url));

                     //throw:
                     //WebClient does not support concurrent I/O operations.
                });

                client.DownloadStringCompleted += (s, ea) =>
                {
                     //Do something
                };

您正在使用一个共享的
WebClient
实例,而计时器显然会导致一次发生多个下载。每次在
勾选
处理程序中启动一个新的客户端实例,或禁用计时器,使其在您仍在处理当前下载时不会再次勾选

timer.Tick += new EventHandler(delegate(object p, EventArgs a)
{
    // Disable the timer so there won't be another tick causing an overlapped request
    timer.IsEnabled = false;

    client.DownloadStringAsync(new Uri(url));                     
});

client.DownloadStringCompleted += (s, ea) =>
{
    // Re-enable the timer
    timer.IsEnabled = true;

    //Do something                
};