4

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();
    }
}
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
Umut Akkaya
  • 73
  • 1
  • 3
  • 7

1 Answers1

11

When calling DownloadFileAsync method you have to make sure it completes before trying to download again.

This answer will help you.

Community
  • 1
  • 1
Damith
  • 62,401
  • 13
  • 102
  • 153
  • 8
    Or create another WebClient instance, it is not that expensive. – Hans Passant Oct 26 '11 at 17:05
  • 1
    @YoYoMa: Welcome to SO! :) Don't bother adding in explanatory text when removing dead links -- if you haven't found a replacement, feel free to just delete the link completely. If the question no longer survives, then also flag it for moderator attention ("Not an Answer") and consider leaving a comment. Answers that are only links are in most danger of being deleted at some point in the future, so we might as well encourage giving full answers (as UnhandledException has done here -- this answer is still useful even without the link). – sarnold Feb 15 '12 at 00:23