0

このコードについてサポートが必要です

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
                };
4

1 に答える 1

1

共有WebClientインスタンスを使用していて、タイマーが原因で、一度に複数のダウンロードが発生していることは明らかです。ハンドラーで毎回新しいクライアントインスタンスをTick起動するか、タイマーを無効にして、現在のダウンロードを処理している間、タイマーが再び作動しないようにします。

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                
};
于 2011-05-26T18:19:05.953 に答える