gpt4 book ai didi

c# - DownloadFileCompleted 事件不会在循环 c# .net 内部触发

转载 作者:太空宇宙 更新时间:2023-11-03 17:06:13 25 4
gpt4 key购买 nike

我有点困惑为什么在文件下载后事件没有触发。

它自己下载的文件非常好。

我假设我使用它的方式存在一些错误,因为事件不会在循环内触发。

感谢任何人能给我的帮助

class DownloadQueue
{
public List<string[]> DownloadItems { get; set; }
public int CurrentDownloads;
public int DownloadInProgress;
string url = @"http://www.google.co.uk/intl/en_uk/images/logo.gif";
bool downloadComplete;

public DownloadQueue()
{
CurrentDownloads = 0;
DownloadItems = new List<string[]>();
Console.Write("new download queue made");
}

public void startDownloading(int maxSimulatiousDownloads)
{
downloadComplete = true;
DownloadInProgress = 0;
WebClient client = new WebClient();
client.DownloadFileCompleted +=
new AsyncCompletedEventHandler(this.downloadCompleteMethod);


while(DownloadInProgress != DownloadItems.Count )
{
if (downloadComplete == true)
{
downloadComplete = false;

client.DownloadFileAsync(new Uri(DownloadItems.ElementAt(DownloadInProgress).ElementAt(0).ToString()), DownloadItems.ElementAt(DownloadInProgress).ElementAt(1).ToString());
}
}
Console.Write("all downloads completed");
}

private void downloadCompleteMethod(object sender, AsyncCompletedEventArgs e)
{
downloadComplete = true;
DownloadInProgress++;
Console.Write("file Downloaded");
}

}

最佳答案

您在哪里等待 DownloadFileAsync() 调用完成?怎么样

manualResetEvent AllDone = new mre(false)

就在 console.WriteLine 之前

AllDone.WaitOne()

下载完成

if (interlocked.decrement(ref downloadComplete) == 0) { AllDone.Set(); }

关于c# - DownloadFileCompleted 事件不会在循环 c# .net 内部触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2268856/

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