c# - wb.DownloadFileAsync 抛出 "WebClient does not support concurrent I/O operations."异常

标签 c# webclient

我需要有关此代码的帮助

#region Events

public class DownloadProgressChangedEventArg
{
    private long _ProgressPercentage;
    private long _BytesReceived;
    private long _TotalBytesToReceive;

    public DownloadProgressChangedEventArg(long BytesReceived, long TotalBytesToReceive)
    {
        _BytesReceived = BytesReceived;
        _TotalBytesToReceive = TotalBytesToReceive;
        _ProgressPercentage = BytesReceived * 100 / (TotalBytesToReceive);
    }

    public long BytesReceived { get { return _BytesReceived; } set { _BytesReceived = value; } }
    public long ProgressPercentage { get { return _ProgressPercentage; } set { _ProgressPercentage = value; } }
    public long TotalBytesToReceive { get { return _TotalBytesToReceive; } set { _TotalBytesToReceive = value; } }
}

public delegate void DownloadProgressChangedEventHandler(Api.GetSong.GetObject.Object Sender, DownloadProgressChangedEventArg e);
public event DownloadProgressChangedEventHandler DownloadProgressChangedEvent;

public class DownloadCompletedEventArg
{
    private bool _Cancelled;
    private Exception _Error;

    public DownloadCompletedEventArg(Exception Error, bool Cancelled)
    {
        _Cancelled = Cancelled;
        _Error = Error;
    }

    public bool Cancelled { get { return _Cancelled; } set { _Cancelled = value; } }
    public Exception Error { get { return _Error; } set { _Error = value; } }

}

public delegate void DownloadCompletedEventHandler(Api.GetSong.GetObject.Object Sender, DownloadCompletedEventArg e);
public event DownloadCompletedEventHandler DownloadCompletedEvent;

#endregion

WebClient wb;
public void DownloadFileAsync(Api.GetSong.GetObject.Object Object, String FileLocation)
{
    String DownloadLink = GetStreamUri(Object);
    String FileTitle = Object.Title + "." + Object.Type;
    String FileLocations = Path.Combine(FileLocation,FileTitle);

    if (!DownloadLink.StartsWith("rtmp"))
    {
        if (wb == null)
        {
            wb = new WebClient();
            wb.DownloadFileCompleted += delegate(object sender, AsyncCompletedEventArgs e) { DownloadCompletedEvent(Object, new DownloadCompletedEventArg(e.Error, e.Cancelled)); };
            wb.DownloadProgressChanged += delegate(object sender, DownloadProgressChangedEventArgs e) { DownloadProgressChangedEvent(Object, new DownloadProgressChangedEventArg(e.BytesReceived, e.TotalBytesToReceive)); };
        }
        wb.DownloadFileAsync(new Uri(DownloadLink), FileLocations);

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

    }
    else
    {
        //Düzenlencek
    }
}

public void DownloadFileCancel()
{
    if (wb.IsBusy && wb != null)
    {
        wb.CancelAsync();
    }
}

最佳答案

调用 DownloadFileAsync 方法时,您必须确保它已完成,然后再尝试再次下载。

This answer会帮助你。

关于c# - wb.DownloadFileAsync 抛出 "WebClient does not support concurrent I/O operations."异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7905445/

相关文章:

c# - 无法添加或更新 Azure AD 应用程序

c# - HttpListener 忽略请求中#(哈希)之后的所有内容

c# - 如何从 BsonDocument 中正确选择元素的值

c# - 如何在 WebClient.DownloadFileAsync 上实现超时

c# - 我如何使用简单的 C# WebClient 使用相同的键(到谷歌翻译)发布多个值

C# 如何将大的 HEX 字符串转换为二进制

c# - FlowDocument 强制分页 (BreakPageBefore)

spring - Spring WebClient 流上传 'POST'

php - 如何为客户提供直接从 CodeIgniter 网站打印收据的选项