//Execute Websites test in parallel mode public static void StartParallel(IProgress <WebsitesTestReportModel> progress) { //Create an instance of the report model object which will be later sent in progress report WebsitesTestReportModel report = new WebsitesTestReportModel(); List <string> websites = PrepareWebsites(); var temp = new object(); Parallel.ForEach <string>(websites, (site) => { //Cretae an instance of the website model which is later passed into the report WebsiteDataModel result = new WebsiteDataModel(); DownloadWebsite(site); result.URI = site; result.isLoaded = true; //lock around access to shared object for thread safity lock (temp) { report.LoadedWebsites.Add(result); } }); //send the report to update the UI report.ProgressArcAngle = 360; progress.Report(report); }
//Execute Websites test in asynchronous mode public static async Task StartAsync(IProgress <WebsitesTestReportModel> progress, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); //Create an instance of the report model object which will be later sent in progress report WebsitesTestReportModel report = new WebsitesTestReportModel(); List <string> websites = PrepareWebsites(); int loadedWebsitesCount = 0; foreach (string site in websites) { //Cretae an instance of the website model which is later passed into the report WebsiteDataModel result = new WebsiteDataModel(); await DownloadWebsiteAsync(site); result.URI = site; result.isLoaded = true; report.LoadedWebsites.Add(result); loadedWebsitesCount++; //Multiply by 360 because of arc shape of progress bar report.ProgressArcAngle = (loadedWebsitesCount * 360) / websites.Count; progress.Report(report); cancellationToken.ThrowIfCancellationRequested(); } }
private void WebsitesTestProgressEvent(object sender, WebsitesTestReportModel e) { WebsitesTestArcAngle = e.ProgressArcAngle; //Convert LoadedWebsites to a new list to prevent "Collection was modified" error foreach (var site in e.LoadedWebsites.ToList()) { if (site.URI.Contains("google") && site.isLoaded) { GoogleLogoSource = new BitmapImage(new Uri(@"pack://*****:*****@"pack://application:,,,/Resources/Images/Logos/YouTubeLogoColorized.png")); } else if (site.URI.Contains("facebook") && site.isLoaded) { FaceBookLogoSource = new BitmapImage(new Uri(@"pack://*****:*****@"pack://application:,,,/Resources/Images/Logos/VSLogoColorized.png")); } else if (site.URI.Contains("spotify") && site.isLoaded) { SpotifyLogoSource = new BitmapImage(new Uri(@"pack://*****:*****@"pack://application:,,,/Resources/Images/Logos/TwitchLogoColorized.png")); } else if (site.URI.Contains("reddit") && site.isLoaded) { RedditLogoSource = new BitmapImage(new Uri(@"pack://*****:*****@"pack://application:,,,/Resources/Images/Logos/MicrosoftLogoColorized.png")); } else if (site.URI.Contains("instagram") && site.isLoaded) { InstagramLogoSource = new BitmapImage(new Uri(@"pack://*****:*****@"pack://application:,,,/Resources/Images/Logos/StackLogoColorized.png")); } } }
//Execute Websites test in parallel async mode public static async Task StartParallelAsync(IProgress <WebsitesTestReportModel> progress, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); //Create an instance of the report model object which will be later sent in progress report WebsitesTestReportModel report = new WebsitesTestReportModel(); List <string> websites = PrepareWebsites(); var temp = new object(); int loadedWebsitesCount = 0; await Task.Run(() => { Parallel.ForEach <string>(websites, (site, loopState) => { //Cretae an instance of the website model which is later passed into the report WebsiteDataModel result = new WebsiteDataModel(); DownloadWebsite(site); result.URI = site; result.isLoaded = true; if (cancellationToken.IsCancellationRequested) { loopState.Stop(); } //lock around access to shared objects for thread safity lock (temp) { //Send the progress report only if other iterations did not stop the loop if (!loopState.IsStopped) { report.LoadedWebsites.Add(result); loadedWebsitesCount++; //Multiply by 360 because of arc shape of progress bar report.ProgressArcAngle = (loadedWebsitesCount * 360) / websites.Count; progress.Report(report); } } }); }); cancellationToken.ThrowIfCancellationRequested(); }
//Execute Websites test in synchronous mode public static void StartSync(IProgress <WebsitesTestReportModel> progress) { //Create an instance of the report model object which will be later sent in progress report WebsitesTestReportModel report = new WebsitesTestReportModel(); List <string> websites = PrepareWebsites(); foreach (string site in websites) { //Cretae an instance of the website model which is later passed into the report WebsiteDataModel result = new WebsiteDataModel(); DownloadWebsite(site); result.URI = site; result.isLoaded = true; report.LoadedWebsites.Add(result); } //send the report to update the UI report.ProgressArcAngle = 360; progress.Report(report); }