我有一个奇怪的问题我无法解决,我有一个表单,我打开另一个表单,只要我打开该表单,因为没有事件要在页面加载后触发,在表单加载事件我设置一个将在5秒后启动的计时器。计时器将触发将下载文件的任务,下载文件将在进度条中更新。问题是我在任务运行时尝试更改的任何内容都不会更新GUI,只会在所有任务完成后更改GUI,请注意进度条会更新。这是我的代码:
private void frm_HosterDownloader_Load(object sender, EventArgs e)
{
StartDownloadTimer = new Timer();
StartDownloadTimer.Tick += StartDownloadTimer_Tick;
StartDownloadTimer.Interval = 5000;
StartDownloadTimer.Start();
}
void StartDownloadTimer_Tick(object sender, EventArgs e)
{
StartDownload();
StartDownloadTimer.Stop();
}
private void StartDownload()
{
int counter = 0;
Dictionary<string, string> hosters = Hosters.GetAllHostersUrls();
progressBar_Download.Maximum = hosters.Count * 100;
progressBar_Download.Minimum = 0;
progressBar_Download.Value = 0;
foreach (KeyValuePair<string, string> host in hosters)
{
//Updating these tow lables never works, only when everything finishes
lbl_FileName.Text = host.Key;
lbl_NumberOfDownloads.Text = (++counter).ToString() + "/" + hosters.Count().ToString();
Task downloadTask = new Task(() =>
{
Downloader downloader = new Downloader(host.Value, string.Format(HostersImagesFolder + @"\{0}.png", IllegalChars(host.Key)));
downloader.HosterName = host.Key;
downloader.DownloadFinished += downloader_DownloadFinished;
downloader.Execute();
});
downloadTask.Start();
downloadTask.Wait();
}
}
void downloader_DownloadFinished(object sender, ProgressEventArgs e)
{
progressBar_Download.Value = progressBar_Download.Value + (int)e.ProgressPercentage;
}
我厌倦了将拖曳标签声明放在任务中,甚至尝试将它们作为参数传递,以便在DownloadFinish事件中更新,但没有运气。
编辑:
以下是Downloader类:
public class Downloader : DownloaderBase
{
public string HosterName { set; get; }
/// <summary>
/// Initializes a new instance of the <see cref="Downloader"/> class.
/// </summary>
/// <param name="hoster">The hoster to download.</param>
/// <param name="savePath">The path to save the video.</param>
/// <param name="bytesToDownload">An optional value to limit the number of bytes to download.</param>
/// <exception cref="ArgumentNullException"><paramref name="video"/> or <paramref name="savePath"/> is <c>null</c>.</exception>
public Downloader(string hosterUrl, string savePath, int? bytesToDownload = null)
: base(hosterUrl, savePath, bytesToDownload)
{ }
/// <summary>
/// Occurs when the downlaod progress of the file file has changed.
/// </summary>
public event EventHandler<ProgressEventArgs> DownloadProgressChanged;
/// <summary>
/// Starts download.
/// </summary>
/// <exception cref="IOException">The video file could not be saved.</exception>
/// <exception cref="WebException">An error occured while downloading the video.</exception>
public override void Execute()
{
this.OnDownloadStarted(new ProgressEventArgs(0, HosterName));
var request = (HttpWebRequest)WebRequest.Create(this.HosterUrl);
if (this.BytesToDownload.HasValue)
{
request.AddRange(0, this.BytesToDownload.Value - 1);
}
try
{
// the following code is alternative, you may implement the function after your needs
request.Timeout = 100000;
request.ReadWriteTimeout = 100000;
request.ContinueTimeout = 100000;
using (WebResponse response = request.GetResponse())
{
using (Stream source = response.GetResponseStream())
{
using (FileStream target = File.Open(this.SavePath, FileMode.Create, FileAccess.Write))
{
var buffer = new byte[1024];
bool cancel = false;
int bytes;
int copiedBytes = 0;
while (!cancel && (bytes = source.Read(buffer, 0, buffer.Length)) > 0)
{
target.Write(buffer, 0, bytes);
copiedBytes += bytes;
var eventArgs = new ProgressEventArgs((copiedBytes * 1.0 / response.ContentLength) * 100, HosterName);
if (this.DownloadProgressChanged != null)
{
this.DownloadProgressChanged(this, eventArgs);
if (eventArgs.Cancel)
{
cancel = true;
}
}
}
}
}
}
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.Timeout)
Execute();
}
this.OnDownloadFinished(new ProgressEventArgs(100, HosterName));
}
}
public abstract class DownloaderBase
{
/// <summary>
/// Initializes a new instance of the <see cref="DownloaderBase"/> class.
/// </summary>
/// <param name="hosterUrl">The video to download/convert.</param>
/// <param name="savePath">The path to save the video/audio.</param>
/// /// <param name="bytesToDownload">An optional value to limit the number of bytes to download.</param>
/// <exception cref="ArgumentNullException"><paramref name="hosterUrl"/> or <paramref name="savePath"/> is <c>null</c>.</exception>
protected DownloaderBase(string hosterUrl, string savePath, int? bytesToDownload = null)
{
if (hosterUrl == null)
throw new ArgumentNullException("video");
if (savePath == null)
throw new ArgumentNullException("savePath");
this.HosterUrl = hosterUrl;
this.SavePath = savePath;
this.BytesToDownload = bytesToDownload;
}
/// <summary>
/// Occurs when the download finished.
/// </summary>
public event EventHandler<ProgressEventArgs> DownloadFinished;
/// <summary>
/// Occurs when the download is starts.
/// </summary>
public event EventHandler<ProgressEventArgs> DownloadStarted;
/// <summary>
/// Gets the number of bytes to download. <c>null</c>, if everything is downloaded.
/// </summary>
public string HosterUrl { get; set; }
/// <summary>
/// Gets the number of bytes to download. <c>null</c>, if everything is downloaded.
/// </summary>
public int? BytesToDownload { get; private set; }
/// <summary>
/// Gets the path to save the video/audio.
/// </summary>
public string SavePath { get; private set; }
/// <summary>
/// Starts the work of the <see cref="DownloaderBase"/>.
/// </summary>
public abstract void Execute();
protected void OnDownloadFinished(ProgressEventArgs e)
{
if (this.DownloadFinished != null)
{
this.DownloadFinished(this, e);
}
}
protected void OnDownloadStarted(ProgressEventArgs e)
{
if (this.DownloadStarted != null)
{
this.DownloadStarted(this, e);
}
}
}
答案 0 :(得分:3)
以这种方式使用任务是没用的:
downloadTask.Start();
downloadTask.Wait();
Wait()
将阻止调用代码并处理事件。您的下载有效地在主GUI线程上执行,阻止它。
解决方案是
//downloadTask.Wait();
你似乎不需要它。
答案 1 :(得分:2)
很少有充分的理由使用线程(您创建的新线程或线程池)来执行IO绑定工作。以下是同步async
方法的Execute
替代方法:
public async Task ExecuteAsync()
{
this.OnDownloadStarted(new ProgressEventArgs(0, HosterName));
var httpClient = new HttpClient();
var request = (HttpWebRequest)WebRequest.Create(this.HosterUrl);
if (this.BytesToDownload.HasValue)
{
request.AddRange(0, this.BytesToDownload.Value - 1);
}
try
{
request.Timeout = 100000;
request.ReadWriteTimeout = 100000;
request.ContinueTimeout = 100000;
var response = await httpClient.SendAsync(request);
var responseStream = await response.Content.ReadAsStreamAsync();
using (FileStream target = File.Open(this.SavePath, FileMode.Create, FileAccess.Write))
{
var buffer = new byte[1024];
bool cancel = false;
int bytes;
int copiedBytes = 0;
while (!cancel && (bytes = await responseStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
await target.WriteAsync(buffer, 0, bytes);
copiedBytes += bytes;
var eventArgs = new ProgressEventArgs((copiedBytes * 1.0 / response.ContentLength) * 100, HosterName);
if (this.DownloadProgressChanged != null)
{
this.DownloadProgressChanged(this, eventArgs);
if (eventArgs.Cancel)
{
cancel = true;
}
}
}
}
}
catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.Timeout)
}
this.OnDownloadFinished(new ProgressEventArgs(100, HosterName));
}
现在,无需使用Task.Wait
或创建新的Task
。 IO绑定工作本质上是异步的。与C#5中新的async-await
关键字结合使用,您可以在整个时间内保持UI响应,因为每个await
都会将控制权交还给调用方法,并释放您的winforms消息泵以进行处理同时更多的消息。
private async void frm_HosterDownloader_Load(object sender, EventArgs e)
{
await Task.Delay(5000);
await StartDownloadAsync();
}
private async Task StartDownloadAsync()
{
int counter = 0;
Dictionary<string, string> hosters = Hosters.GetAllHostersUrls();
progressBar_Download.Maximum = hosters.Count * 100;
progressBar_Download.Minimum = 0;
progressBar_Download.Value = 0;
var downloadTasks = hosters.Select(hoster =>
{
lbl_FileName.Text = hoster.Key;
lbl_NumberOfDownloads.Text = (++counter).ToString() + "/" + hosters.Count().ToString();
Downloader downloader = new Downloader(host.Value, string.Format(HostersImagesFolder + @"\{0}.png", IllegalChars(host.Key)));
downloader.HosterName = host.Key;
downloader.DownloadFinished += downloader_DownloadFinished;
return downloader.ExecuteAsync();
});
return Task.WhenAll(downloadTasks);
}
注意我将你的计时器更改为Task.Delay
,因为它在内部使用计时器而你只需执行一次。
如果您想要更多地使用async-await
,可以启动here。