Esempio n. 1
0
        private void RunDownloadSync()
        {
            List <string> websites = PrepData();

            foreach (string site in websites)
            {
                WebsiteDataModel results = DownloadWebsite(site);
                ReportWebsiteInfo(results);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Here is shown how a method can return a Task wrapped with data.
        /// The Task contains the WebsiteDataModel and it can be used in RunDownloadParallelAsync() method
        /// </summary>
        /// <param name="websiteURL"></param>
        /// <returns></returns>
        private async Task <WebsiteDataModel> DownloadWebsiteAsync(string websiteURL)
        {
            WebsiteDataModel output = new WebsiteDataModel();
            WebClient        client = new WebClient();

            output.WebsiteUrl  = websiteURL;
            output.WebsiteData = await client.DownloadStringTaskAsync(websiteURL);

            return(output);
        }
        private static WebsiteDataModel DownloadWebsite(string websiteURL)
        {
            WebsiteDataModel output = new WebsiteDataModel();
            WebClient        client = new WebClient();

            output.WebsiteUrl  = websiteURL;
            output.WebsiteData = client.DownloadString(websiteURL);

            return(output);
        }
Esempio n. 4
0
        private WebsiteDataModel DownloadWebsite(string websiteURL)
        {
            var output = new WebsiteDataModel();
            var client = new WebClient();

            output.WebsiteUrl  = websiteURL;
            output.WebsiteData = client.DownloadString(websiteURL);

            return(output);
        }
Esempio n. 5
0
        /// <summary>
        /// Method that downloads website data asynchronus.
        /// Notice that it returns a Task and has the keyword async.
        /// The await is used for the Task to wait for a method to be done executing. And the UI can be responsive while this is happening.
        /// </summary>
        /// <returns></returns>
        private async Task RunDownloadAsync()
        {
            List <string> websites = PrepData();

            foreach (string site in websites)
            {
                WebsiteDataModel results = await Task.Run(() => DownloadWebsite(site));

                ReportWebsiteInfo(results);
            }
        }
Esempio n. 6
0
        public static List <WebsiteDataModel> RunDownloadSync()
        {
            List <string>           websites = PrepData();
            List <WebsiteDataModel> output   = new List <WebsiteDataModel>();

            foreach (string site in websites)
            {
                WebsiteDataModel results = DownloadWebsite(site);
                output.Add(results);
            }
            return(output);
        }
Esempio n. 7
0
        private async Task RunDownloadAsync()
        {
            //this method is don't provide any performance optimitation because the ask for a site and the wait for the interrogation.
            List <string> websites = PrepData();

            foreach (string site in websites)
            {
                WebsiteDataModel results = await Task.Run(() => DownloadWebsite(site));

                ReportWebsiteInfo(results);
            }
        }
Esempio n. 8
0
        private WebsiteDataModel DownloadWebsite(string websiteUrl)
        {
            WebsiteDataModel output = new WebsiteDataModel();
            WebClient        client = new WebClient();

            output.WebsiteUrl = websiteUrl;
            var result = client.DownloadString(websiteUrl);

            output.WebsiteData = result;
            output.getResults  = !string.IsNullOrEmpty(result);
            return(output);
        }
        public static List <WebsiteDataModel> RunDownloadParallelSync()
        {
            List <string>           websites = PrepData();
            List <WebsiteDataModel> output   = new List <WebsiteDataModel>();

            Parallel.ForEach <string>(websites, (site) =>
            {
                WebsiteDataModel results = DownloadWebsite(site);
                output.Add(results);
            });

            return(output);
        }
Esempio n. 10
0
        public static List <WebsiteDataModel> RunDownloadSync(System.Windows.Controls.ProgressBar dashboardProgress)
        {
            List <string>           websites = PrepData();
            List <WebsiteDataModel> output   = new List <WebsiteDataModel>();

            foreach (string site in websites)
            {
                WebsiteDataModel results = DownloadWebsite(site);
                output.Add(results);
                dashboardProgress.Dispatcher.Invoke(() => dashboardProgress.Value = output.Count * 100 / websites.Count, DispatcherPriority.Background);
            }

            return(output);
        }
Esempio n. 11
0
        private async Task RunDownloadAsync()
        {
            List <string> websites = PrepData();

            foreach (string site in websites)
            {
                // it will block next website until the 1st website done
                // obs:optimize: never keep the await in foreach
                //u can make entire method async thats great else ts.run(method()) both same st is preferrable
                WebsiteDataModel results = await Task.Run(() => DownloadWebsite(site));

                ReportWebsiteInfo(results);
            }
        }
Esempio n. 12
0
        //Never return void from an async task(only by an event thats an exception)
        private async Task RunDownloadAsync()
        {
            List <string> websites = PrepData();

            foreach (string site in websites)
            {
                //Do that asynchronosly but wait for the result
                //The caller will go on with his work while this is running
                //When he gets the ressults he makes further here
                WebsiteDataModel results = await Task.Run(() => DownloadWebsite(site));

                ReportWebsiteInfo(results);
            }
        }
Esempio n. 13
0
        private async Task <WebsiteDataModel> DownloadWebsiteAsync(string websiteUrl)
        {
            WebsiteDataModel output = new WebsiteDataModel();

            output.WebsiteUrl = websiteUrl;
            var stopwatch = System.Diagnostics.Stopwatch.StartNew();
            var result    = await HttpClient.GetStringAsync(websiteUrl);

            output.getResults = !string.IsNullOrEmpty(result);

            output.WebsiteData       = result;
            output.ThreadElapsedTime = $"{stopwatch.ElapsedMilliseconds}";
            return(output);
        }
Esempio n. 14
0
        /// <summary>
        /// If we don't have control over the synchronous method 'DownloadWebsite'
        /// but we still need to run it asyncronously then use Task.Run.
        /// Tip: Don't return void from an async method. If there's nothing to return, use 'Task'
        /// Or if there's a return type e.g. string, use Task<string>
        /// One exception: In case of event leave the return type void.
        /// Naming convention: Append 'Async' to the end of the methodname which going to return asynchronously.
        /// 'async' means the caller of this method can go on with their task!
        ///
        /// </summary>
        /// <returns></returns>
        private async Task RunDownloadAsync()
        {
            List <string> websites = PrepData();

            foreach (string site in websites)
            {
                //Starts DownloadWebsite method and waits it to return
                //Loads sites one by one
                //This cause similar execution time as synchronous RunDownloadSync()
                //But at least the UI will be responsive
                WebsiteDataModel results = await Task.Run(() => DownloadWebsite(site));

                ReportWebsiteInfo(results);
            }
        }
Esempio n. 15
0
        public static List <WebsiteDataModel> RunDownloadParallelSync()
        {
            List <string>           websites = PrepData();
            List <WebsiteDataModel> output   = new List <WebsiteDataModel>();

            // Inside the foreach code execution is parralell, not iteration by iteration
            // It locks up until the longest task is ready, so the UI is inresponsive.
            Parallel.ForEach <string>(websites, (site) =>
            {
                WebsiteDataModel results = DownloadWebsite(site);
                output.Add(results);
            });

            return(output);
        }
Esempio n. 16
0
        public static async Task <List <WebsiteDataModel> > RunDownloadParallelAsyncV2()
        {
            List <string>           websites = PrepData();
            List <WebsiteDataModel> output   = new List <WebsiteDataModel>();

            await Task.Run(() =>
            {
                Parallel.ForEach <string>(websites, (site) =>
                {
                    WebsiteDataModel results = DownloadWebsite(site);
                    output.Add(results);
                });
            });

            return(output);
        }
        private static WebsiteDataModel DownloadWebsite(string websiteURL)
        {
            WebProxy p = new WebProxy("10.216.190.6", true);

            p.Credentials = new NetworkCredential("john.reese", "john.reese");
            WebRequest.DefaultWebProxy = p;

            WebsiteDataModel output = new WebsiteDataModel();
            WebClient        client = new WebClient();

            client.Proxy = p;

            output.WebsiteUrl  = websiteURL;
            output.WebsiteData = client.DownloadString(websiteURL);

            return(output);
        }
Esempio n. 18
0
        public static async Task <List <WebsiteDataModel> > RunDownloadAsync(IProgress <ProgressReportModel> progress)
        {
            List <string>           websites = PrepData();
            List <WebsiteDataModel> output   = new List <WebsiteDataModel>();
            ProgressReportModel     report   = new ProgressReportModel();

            foreach (string site in websites)
            {
                WebsiteDataModel results = await DownloadWebsiteAsync(site);

                output.Add(results);
                report.PercentageComplete = output.Count * 100 / websites.Count;
                report.SitesDownloaded    = output;
                progress.Report(report);
            }

            return(output);
        }
Esempio n. 19
0
        // Using Parallel ForEach
        public static List <WebsiteDataModel> RunDownloadParallelSync()
        {
            List <string>           websites = PrepData();
            List <WebsiteDataModel> output   = new List <WebsiteDataModel>();

            // Parallel ForEach:
            // foreach (string site in website)
            // {
            //    WebsiteDataModel results = DownloadWebsite(site);
            //    output.Add(results);
            //  }
            Parallel.ForEach <string>(websites, (site) =>
            {
                WebsiteDataModel results = DownloadWebsite(site);
                output.Add(results);
            });

            return(output);
        }//RunDownloadParallelSync
Esempio n. 20
0
        }//RunDownloadParallelSync

        // Wrapping Parallel.ForEach to an Async Task
        public static async Task <List <WebsiteDataModel> > RunDownloadParallelAsyncV2(IProgress <ProgressReportModel> progress)
        {
            List <string>           websites = PrepData();
            List <WebsiteDataModel> output   = new List <WebsiteDataModel>();
            ProgressReportModel     report   = new ProgressReportModel();

            await Task.Run(() =>
            {
                Parallel.ForEach <string>(websites, (site) =>
                {
                    WebsiteDataModel results = DownloadWebsite(site);
                    output.Add(results);

                    report.SitesDownloaded    = output;
                    report.PercentageComplete = (output.Count * 100) / websites.Count; // converting to percent values
                    progress.Report(report);
                });
            });

            return(output);
        }//RunDownloadParallelAsyncV2
Esempio n. 21
0
        public static async Task <List <WebsiteDataModel> > RunDownloadParallelAsync(IProgress <ProgressReportModel> progress)
        {
            List <string>           websites        = PrepData();
            List <WebsiteDataModel> output          = new List <WebsiteDataModel>();
            ProgressReportModel     report          = new ProgressReportModel();
            ParallelOptions         parallelOptions = new ParallelOptions();

            parallelOptions.MaxDegreeOfParallelism = 6;
            await Task.Run(() => {
                Parallel.ForEach <string>(websites, parallelOptions, (site) =>
                {
                    WebsiteDataModel results = DownloadWebsite(site);
                    output.Add(results);
                    report.PercentageComplete = output.Count * 100 / websites.Count;
                    report.SitesDownloaded    = output;
                    progress.Report(report);
                });
            });

            return(output);
        }
Esempio n. 22
0
        public static async Task <List <WebsiteDataModel> > RunDownloadAsync(IProgress <ProgressReportModel> progress, CancellationToken cancellationToken)
        {
            List <string>           websites            = PrepData();
            List <WebsiteDataModel> output              = new List <WebsiteDataModel>();
            ProgressReportModel     progressReportModel = new ProgressReportModel();

            foreach (string site in websites)
            {
                WebsiteDataModel results = await DownloadWebsiteAsync(site);

                output.Add(results);

                cancellationToken.ThrowIfCancellationRequested();

                progressReportModel.SitesDownloaded    = output;
                progressReportModel.PercentageComplete = (output.Count * 100) / websites.Count;

                progress.Report(progressReportModel);
            }

            return(output);
        }
Esempio n. 23
0
        }//RunDownloadParallelAsyncV2

        public static async Task <List <WebsiteDataModel> > RunDownloadAsync(IProgress <ProgressReportModel> progress, CancellationToken cancellationToken)
        {
            List <string>           websites = PrepData();
            List <WebsiteDataModel> output   = new List <WebsiteDataModel>();
            ProgressReportModel     report   = new ProgressReportModel();

            foreach (string site in websites)
            {
                WebsiteDataModel results = await DownloadWebsiteAsync(site);

                output.Add(results);

                // TODO implement if to check IsCancellationRequested method
                // Adding Cancelling functionaly if page takes too long to download, Won't cancel unless the task is cancelled (clicked)
                cancellationToken.ThrowIfCancellationRequested();

                report.SitesDownloaded    = output;
                report.PercentageComplete = (output.Count * 100) / websites.Count; // converting to percent values
                progress.Report(report);
            }

            return(output);
        }// RunDownloadAsync
Esempio n. 24
0
 private void ReportWebsiteInfo(WebsiteDataModel data)
 {
     resultsWindow.Text += $"{ data.WebsiteUrl } downloaded: { data.WebsiteData.Length } characters long.{ Environment.NewLine }";
 }
Esempio n. 25
0
 private void ReportWebsiteInfo(WebsiteDataModel data)
 {
     resultsWindowBlack.Text += $"{ data.WebsiteUrl } (DL: { data.WebsiteData.Length } bytes). Time: {data.ThreadElapsedTime} ms. { Environment.NewLine }";
     resultsWindowGreen.Text += $"\t {(data.getResults ? "OK!" : "FAILED!")} { Environment.NewLine }";
 }