First off you need to await this asyncImageDowloader.DownloaderAsync();
And Main signature static async Task Main(string[] args)
But you have not indicated what the error is.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello. I learn c#, a topic about asynchrony. I wrote a code that downloads 10 pictures at the same time. In the main method, the user must check if the images are loaded. Or press button A and close all streams. When executed, it does not download all images (only the last image is saved in the program file), but when I did debugging, the program saved all the images. Can you explain why this happens? Why does an error occur during fast work? Can you give advice on how to improve the code?
And I know that using WebClient is outdated, but I wanted to practice on it.
namespace TheNinthProgram
{
internal class Program
{
static void Main(string[] args)
{
static void c_ImageStarted(object sender, EventArgs e) => Console.WriteLine("File download started");
static void c_ImageCompleted(object sender, EventArgs e) => Console.WriteLine("File download finished");
string urlOfImage = "https://i.pinimg.com/originals/af/84/21/af8421d076ff051a9bea38f45f162edf.jpg";
List<string> listOfUrl = new List<string>();
for (int i = 0; i < 10; i++)
{
listOfUrl.Add(urlOfImage);
}
ImageDownloaderAsync asyncImageDowloader= new (listOfUrl);
asyncImageDowloader.ImageStarted += c_ImageStarted;
asyncImageDowloader.ImageCompleted -= c_ImageCompleted;
asyncImageDowloader.DownloaderAsync();
Console.WriteLine("Press the A key to exit or any other key to check the download status");
ConsoleKeyInfo cki = Console.ReadKey();
if (cki.Key == ConsoleKey.A) Environment.Exit(0);
else
{
Console.WriteLine(asyncImageDowloader.IsCompleted? "\n Loaded":"\n Not loaded");
}
}
internal class ImageDownloaderAsync
{
public event EventHandler ImageStarted;
public event EventHandler ImageCompleted;
CancellationTokenSource _cancellationToken;
string _fileName;
int i = 1;
List<string> _listOfUriOfImages;
public bool IsCompleted { get; private set; }
public ImageDownloaderAsync(List<string> listOfUriOfImages)
{
_listOfUriOfImages = listOfUriOfImages;
}
/// <summary>
/// The method that downloads the image.
/// </summary>
public async Task DownloaderAsync()
{
var tasks = new List<Task>();
foreach (var url in _listOfUriOfImages)
{
_fileName = $"image{i}.jpg";
i++;
try
{
tasks.Add(Task.Run(async () =>
{
ImageStarted?.Invoke(this, EventArgs.Empty);
WebClient myWebClient = new WebClient();
await myWebClient.DownloadFileTaskAsync(url, _fileName);
});
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
try
{
await Task.WhenAll(tasks.ToArray());
ImageCompleted?.Invoke(this, EventArgs.Empty);
IsCompleted = true;
}
catch(Exception ex)
{
Console.WriteLine("Download error");
Console.WriteLine(ex.Message);
}
}
}
}
First off you need to await this asyncImageDowloader.DownloaderAsync();
And Main signature static async Task Main(string[] args)
But you have not indicated what the error is.
Hi, vitaminchik
Use snippet to get detailed error log
catch (Exception ex)
{
Console.WriteLine("Download error");
while (ex != null)
{
Console.WriteLine(ex.Message);
ex = ex.InnerException;
}
}
Modified:
Use filename as a temporary variable
var _fileName = $"image{i}.jpg";
Best regards,
Minxin Yu
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.