예제 #1
0
 /// <summary>
 /// Cancels a download by closing the <see cref="StreamReader"/> created by <see cref="Download(string)"/>
 /// </summary>
 public void Cancel()
 {
     if (_reader != null)
     {
         _reader.Close();
         _reader.Dispose();
         EndedDownload?.Invoke(this, new DownloadEventArgs("User cancelled download", false, true));
     }
 }
예제 #2
0
 /// <summary>
 /// Asynchronously downloads data from a the <paramref name="url"/> url.
 /// </summary>
 /// <param name="url">url from wich to download</param>
 /// <returns></returns>
 public async Task Download(string url)
 {
     IsDownloading?.Invoke(this, new DownloadEventArgs(url, false, false));
     try
     {
         WebRequest  request  = WebRequest.Create(url);
         WebResponse response = request.GetResponse();
         using (Stream dataStream = response.GetResponseStream())
         {
             _reader        = new StreamReader(dataStream);
             DownloadedData = await _reader.ReadToEndAsync();
         }
         response.Close();
         Url = url;
         EndedDownload?.Invoke(this, new DownloadEventArgs("Finished downloading : " + url, false, false));
     }
     catch (UriFormatException e)
     {
         EndedDownload?.Invoke(this, new DownloadEventArgs(e.Message, true, false));
     }
 }