2011-10-26 9 views
3

は、私はあなたがそれを再度ダウンロードしようとする前に完了したことを確認する必要がありDownloadFileAsyncメソッドを呼び出す場合、このコードwb.DownloadFileAsync throw "WebClientは同時I/O操作をサポートしていません。例外

#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(); 
    } 
} 

答えて

9

を支援する必要があります。

This answerがお手伝いします。

+7

別のWebClientインスタンスを作成しますが、それほど高価ではありません。 –

+1

@YoYoMa:ようこそ! :)デッドリンクを削除するときに説明文を追加する必要はありません。置換えが見つからない場合は、リンクを完全に削除するだけです。質問がもはや残っていない場合は、それを司会者の注意を喚起するためにフラグを立てる(「回答ではない」)とコメントを残すことを検討する。リンクだけであるアンサーは、将来的にある時点で削除される可能性が最も高いので、完全な答えを与えることを奨励するかもしれません(UnhandledExceptionがここで行ったように - この答えはリンクなしでも有用です)。 – sarnold

関連する問題