I have the following code that will download a file asynchronously to my hard-drive, shouting to the console the current progress and quitting with a goodbye message in the end:
webClient.DownloadProgressChanged.Add(fun args ->
if (currentPercentage < args.ProgressPercentage) then
Console.WriteLine(args.ProgressPercentage.ToString() + "%")
currentPercentage <- args.ProgressPercentage
)
webClient.DownloadFileCompleted.Add(fun args ->
Console.WriteLine("Download finished!")
Environment.Exit 0
)
webClient.DownloadFileAsync(new Uri(url_to_download), file_name)
Thread.Sleep Int32.MaxValue
I was wondering, though, whether there could be any more elegant way of achieving this without having to resort to “sleeping forever” in the main thread, having the program end though an Environment.Exit(). I have no prejudice towards using Environment.Exit() but I’d like to avoid it, if possible! The only way I can think of avoiding this would be to spawn a new thread and then waiting for it to die, but that does seem cumbersome. Any simpler way to accomplish this?
You can use a ResetEvent like this: