Answer to Question #189425 in C# for CHANDRASENA REDDY

Question #189425

The code has some bugs. It is supposed to raise an event, whenever the File is downloaded


delegate void DownloadCompeteHandler(int perc);

public class FileDownloader

{

protected string resourceUrl;

protected string resourceSavePath;

event DownloadCompeteHandler DownLoadComplete;

public FileDownloader(string url,string savepath)

{

this.resourceUrl = url;

this. resourceSavePath = savepath;

}

public void DownLoadResource()

{

for (int i = 1; i <= 4; i++)

{

for (int j = 1; i <= 10000; i++);

OnDownLoadComplete(i * 25);

}

}

protected void OnDownLoadComplete( )

{

if (DownLoadComplete == null)

DownLoadComplete( );

}

}

public static void Main(string[] args)

{

FileDownloader fd =

new FileDownloader("http://www.microsoft.com/vstudio/expressv10.zip", "d:\\setups");

fd.DownLoadComplete += new Handler();

fd.DownLoadResource();

Console.ReadKey();

}

static void fd_DownLoadComplete(int perc)

{

Console.SetCursorPosition(10, 10);

Console.Write("Downloading {0} Percent Complete", perc);

}


1
Expert's answer
2021-05-13T23:24:50-0400
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Threading;


namespace C_SHARP
{
    class Program
    {


        public delegate void DownloadCompeteHandler(int perc);


        public class FileDownloader
        {
            
            protected string resourceUrl;


            protected string resourceSavePath;


            public event DownloadCompeteHandler DownLoadComplete;


            public FileDownloader(string url, string savepath)
            {


                this.resourceUrl = url;


                this.resourceSavePath = savepath;


            }


            public void DownLoadResource()
            {


                for (int i = 1; i <= 4; i++)
                {
                    OnDownLoadComplete(i * 25);
                    int milliseconds = 2000;
                    Thread.Sleep(milliseconds);
                }


            }


            protected void OnDownLoadComplete(int k)
            {




                if (DownLoadComplete != null) {
                    DownLoadComplete(k);
                }
                
            }


        }


        public static void Main(string[] args)
        {


            FileDownloader fd =new FileDownloader("http://www.microsoft.com/vstudio/expressv10.zip", "d:\\setups");
            fd.DownLoadComplete += fd_DownLoadComplete;


            fd.DownLoadResource();


            Console.ReadKey();


        }


        static void fd_DownLoadComplete(int perc)
        {


            Console.SetCursorPosition(10, 10);
            Console.Write("Downloading {0} Percent Complete", perc);


        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS