Why is my console application code throwing an error?

vitaminchik 486 Reputation points
2023-03-26T22:21:39.02+00:00

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);
            }
        }
    }
}
Developer technologies | C#
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2023-03-27T01:00:50.7433333+00:00

    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.


  2. Minxin Yu 13,506 Reputation points Microsoft External Staff
    2023-03-28T03:26:28.11+00:00

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.