wb.DownloadFileAsync扔" Web客户端不支持并发I / O操作"。例外 [英] wb.DownloadFileAsync throw "WebClient does not support concurrent I/O operations." exception
本文介绍了wb.DownloadFileAsync扔" Web客户端不支持并发I / O操作"。例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用此代码
#地区的活动
公共类DownloadProgressChangedEventArg $ B $帮助b {
私人长期_ProgressPercentage;
私人长期_BytesReceived;
私人长期_TotalBytesToReceive;
公共DownloadProgressChangedEventArg(长与BytesReceived,长TotalBytesToReceive)
{
_BytesReceived =与BytesReceived;
_TotalBytesToReceive = TotalBytesToReceive;
_ProgressPercentage = *与BytesReceived 100 /(TotalBytesToReceive);
}
众长{与BytesReceived获得{返回_BytesReceived; }集合{_BytesReceived =价值; }}
众长ProgressPercentage {{返回_ProgressPercentage; }集合{_ProgressPercentage =价值; }}
众长TotalBytesToReceive {{返回_TotalBytesToReceive; }集合{_TotalBytesToReceive =价值; }}
}
公众委托无效DownloadProgressChangedEventHandler(Api.GetSong.GetObject.Object发件人,DownloadProgressChangedEventArg E);
公共事件DownloadProgressChangedEventHandler DownloadProgressChangedEvent;
公共类DownloadCompletedEventArg
{
私人布尔_Cancelled;
私人异常_Error;
公共DownloadCompletedEventArg(异常错误,BOOL取消)
{
_Cancelled =取消;
_Error =错误;
}
公共BOOL取消{{返回_Cancelled; }集合{_Cancelled =价值; }}
公共异常错误{{返回_Error; }集合{_Error =价值; }}
}
公众委托无效DownloadCompletedEventHandler(Api.GetSong.GetObject.Object发件人,DownloadCompletedEventArg E);
公共事件DownloadCompletedEventHandler DownloadCompletedEvent;
#endregion
Web客户端WB;
公共无效DownloadFileAsync(Api.GetSong.GetObject.Object对象,字符串FileLocation)
{
字符串DownloadLink = GetStreamUri(对象);
字符串FileTitle = Object.Title +。 + Object.Type;
字符串FileLocations = Path.Combine(FileLocation,FileTitle);
如果(!DownloadLink.StartsWith(RTMP))
{
如果(WB == NULL)
{
WB =新的Web客户端( );
wb.DownloadFileCompleted + =委托(对象发件人,AsyncCompletedEventArgs E){DownloadCompletedEvent(对象,新DownloadCompletedEventArg(e.Error,e.Cancelled)); };
wb.DownloadProgressChanged + =委托(对象发件人,DownloadProgressChangedEventArgs E){DownloadProgressChangedEvent(对象,新DownloadProgressChangedEventArg(e.BytesReceived,e.TotalBytesToReceive)); };
}
wb.DownloadFileAsync(新的URI(DownloadLink),FileLocations);
//抛出:
// Web客户端不支持并发I / O操作。
}
,否则
{
//Düzenlencek
}
}
公共无效DownloadFileCancel()
{
如果(wb.IsBusy&安培;&安培; WB!= NULL)
{
wb.CancelAsync();
}
}
解决方案
当调用DownloadFileAsync方法,你必须确保它尝试重新下载之前完成。
的这个答案会帮助你。
I need help with this code
#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();
}
}
解决方案
When calling DownloadFileAsync method you have to make sure it completes before trying to download again.
This answer will help you.
这篇关于wb.DownloadFileAsync扔" Web客户端不支持并发I / O操作"。例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文