wb.DownloadFileAsync抛出"WebClient不支持并发I/O操作"异常。

4

我需要帮助解决这段代码

#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();
    }
}
1个回答

11

在调用DownloadFileAsync方法之前,您必须确保它在尝试再次下载之前已经完成。

这个答案会对您有所帮助。


8
或者创建另一个WebClient实例,这不是很昂贵。 - Hans Passant
1
@YoYoMa:欢迎来到SO! :) 当删除死链接时不必添加解释性文本 - 如果您没有找到替代品,可以随意完全删除链接。如果问题不再存在,则还应将其标记为管理员注意事项(“非答案”),并考虑留下评论。仅为链接的答案最有可能在未来某个时候被删除,因此我们最好鼓励提供完整的答案(就像UnhandledException在这里所做的那样 - 即使没有链接,此答案仍然有用)。 - sarnold

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接