gpt4 book ai didi

c# - 如何给下载器添加停止按钮

转载 作者:太空宇宙 更新时间:2023-11-03 23:18:47 28 4
gpt4 key购买 nike

我正在为我的应用程序开发一个下载器管理器,但我不知道如何使“停止”按钮起作用我正在搜索解决方案,但我找不到任何可以帮助我的东西代码是[c#]

private void btnDownload_Click(object sender, EventArgs e)
{
btnDownload.Enabled = false;
btnStop.Enabled = true;
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
webClient.DownloadFileAsync(new Uri(url.Text), path.Text ; )
}

private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
}

private void Completed(object sender, AsyncCompletedEventArgs e)
{
MessageBox.Show("Download completed!");
}

private void btnstop (object sender , e)
{
btnDownload.Enabled = true;
btnstop.Enabled = false;
progressbar.value = 0;
}

最佳答案

作为Nitro.de说,你应该使用 WebClient.CancelAsync .

Cancels a pending asynchronous operation.

并记住检查 e.Cancelled 是否为真

private void Completed(object sender, AsyncCompletedEventArgs e)
{
if(e.Cancelled)
MessageBox.Show("Download cancelled!");
else
MessageBox.Show("Download completed!");
}

关于c# - 如何给下载器添加停止按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36199644/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com