Как установить TimeOut для WebClient на .net?
Я загружаю некоторый файл, но я также хочу установить тайм-аут для веб-клиента. Как я вижу, никаких изменений просто нет. Мы можем использовать переопределение WebRequest. Я уже сделал, но это не работает. Я имею в виду, что переопределение метода GetWebRequest не работает .. Вот мои коды
public class VideoDownloader : Downloader
{
///
/// Initializes a new instance of the class.
///
/// The video to download.
/// The path to save the video.
public VideoDownloader(VideoInfo video, string savePath)
: base(video, savePath)
{ }
///
/// Starts the video download.
///
public override void Execute()
{
// We need a handle to keep the method synchronously
var handle = new ManualResetEvent(false);
var client = new WebClient();
client.DownloadFileCompleted += (sender, args) => handle.Set();
client.DownloadProgressChanged += (sender, args) =>
this.OnProgressChanged(new ProgressEventArgs(args.ProgressPercentage));
this.OnDownloadStarted(EventArgs.Empty);
client.DownloadFileAsync(new Uri(this.Video.DownloadUrl), this.SavePath);
handle.WaitOne();
handle.Close();
this.OnDownloadFinished(EventArgs.Empty);
}
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest w = base.GetWebRequest(address);
w.Timeout = 10*1000; // 20 * 60 * 1000;
return w;
}
}
И класс загрузки
public abstract class Downloader: WebClient
{
///
/// Initializes a new instance of the class.
///
/// The video to download/convert.
/// The path to save the video/audio.
protected Downloader(VideoInfo video, string savePath)
{
this.Video = video;
this.SavePath = savePath;
}
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest w = base.GetWebRequest(address);
w.Timeout = 10 * 1000; // 20 * 60 * 1000;
return w;
}
///
/// Occurs when the download finished.
///
public event EventHandler DownloadFinished;
///
/// Occurs when the download is starts.
///
public event EventHandler DownloadStarted;
///
/// Occurs when the progress has changed.
///
public event EventHandler ProgressChanged;
///
/// Gets the path to save the video/audio.
///
public string SavePath { get; private set; }
///
/// Gets the video to download/convert.
///
public VideoInfo Video { get; private set; }
///
/// Starts the work of the .
///
public abstract void Execute();
protected void OnDownloadFinished(EventArgs e)
{
if (this.DownloadFinished != null)
{
this.DownloadFinished(this, e);
}
}
protected void OnDownloadStarted(EventArgs e)
{
if (this.DownloadStarted != null)
{
this.DownloadStarted(this, e);
}
}
protected void OnProgressChanged(ProgressEventArgs e)
{
if (this.ProgressChanged != null)
{
this.ProgressChanged(this, e);
}
}
}
Где моя ошибка? Примечание: я хочу сделать асинхронность загрузки