Пример #1
0
        /// <summary>
        /// Following are the functions that contain the logic that is executed once a button is pressed
        /// </summary>
        #region Buttons Logic
        private void SyncTest()
        {
            TotalTimeWatch.Start();

            //Initialize instances of Progress class that are later used to trigger
            //the events that update UI
            Progress <ImageTestReportModel> imageTestProgress = new Progress <ImageTestReportModel>();

            imageTestProgress.ProgressChanged += ImageTestProgressEvent;
            Progress <PNumTestReportModel> pNumTestProgress = new Progress <PNumTestReportModel>();

            pNumTestProgress.ProgressChanged += PNumTestProgressEvent;
            Progress <WebsitesTestReportModel> websitesTestProgress = new Progress <WebsitesTestReportModel>();

            websitesTestProgress.ProgressChanged += WebsitesTestProgressEvent;

            //Start executing each test sequentially
            ImageTestWatch.Start();
            ImageTest.StartSync(imageTestProgress);
            ImageTestWatch.Stop();
            ImageTestCheckmarkVisibility = Visibility.Visible;

            PNumTestWatch.Start();
            PrimeNumbersTest.StartSync(pNumTestProgress, pNumRangeTest, pNumNthTest);
            PNumTestWatch.Stop();
            PNumTestCheckmarkVisibility = Visibility.Visible;

            WebsitesTestWatch.Start();
            try
            {
                WebsitesTest.StartSync(websitesTestProgress);
                WebsitesTestCheckmarkVisibility = Visibility.Visible;
            }
            catch (WebException)
            {
                WebsitesTestProgressBarColour   = new SolidColorBrush(Colors.Red);
                WebsitesTestCrossmarkVisibility = Visibility.Visible;
            }
            WebsitesTestWatch.Stop();

            //Enable Reset button to let the user observe the results
            //and then reset UI before next run
            SyncBtnIsEnabled  = ParallelBtnIsEnabled = false;
            ResetBtnIsEnabled = true;

            //Stop Total timer and update all timers
            TotalTimeWatch.Stop();
            UpdateTimers(this, new EventArgs());

            //Push the results of execution to the results manager
            ResultsDataManager.SetResults("Synchronous", ImageTestTimerText, PNumTestTimerText, WebsitesTestTimerText, TotalTimeText, true);
        }
Пример #2
0
        private async void ParallelAsyncTest()
        {
            //disable UI elements
            ResetUI();
            SyncBtnIsEnabled    = AsyncBtnIsEnabled = ParallelBtnIsEnabled = ParallelAsyncBtnIsEnabled = false;
            CancelBtnIsEnabled  = true;
            ParallelAsyncBtnTag = "Clicked";

            DTime.Start();
            TotalTimeWatch.Start();

            //Initialize instances of Progress class that are later used to trigger
            //the events that update UI
            Progress <ImageTestReportModel> imageTestProgress = new Progress <ImageTestReportModel>();

            imageTestProgress.ProgressChanged += ImageTestProgressEvent;
            Progress <PNumTestReportModel> pNumTestProgress = new Progress <PNumTestReportModel>();

            pNumTestProgress.ProgressChanged += PNumTestProgressEvent;
            Progress <WebsitesTestReportModel> websitesTestProgress = new Progress <WebsitesTestReportModel>();

            websitesTestProgress.ProgressChanged += WebsitesTestProgressEvent;


            //Start executing all 3 tests asynchronously and in parallel
            Task ImageTestTask = Task.Run(async() =>
            {
                try
                {
                    ImageTestWatch.Start();
                    await ImageTest.StartAsync(imageTestProgress, cts.Token);
                    ImageTestCheckmarkVisibility = Visibility.Visible;
                    ImageTestWatch.Stop();
                }
                catch (OperationCanceledException)
                {
                    //Call dispatcher to update UI from worker thread
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        ImageTestProgressBarColour   = new SolidColorBrush(Colors.Red);
                        ImageTestCrossmarkVisibility = Visibility.Visible;
                        CancelBtnTag = null;
                    });
                }
            });

            Task NthNumberTestTask = Task.Run(async() =>
            {
                try
                {
                    PNumTestWatch.Start();
                    await PrimeNumbersTest.StartParallelAsync(pNumTestProgress, pNumRangeTest, pNumNthTest, cts.Token);
                    PNumTestWatch.Stop();
                    PNumTestCheckmarkVisibility = Visibility.Visible;
                }
                catch (OperationCanceledException)
                {
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        PNumTestProgressBarColour   = new SolidColorBrush(Colors.Red);
                        PNumTestCrossmarkVisibility = Visibility.Visible;
                        CancelBtnTag = null;
                    });
                }
            });

            Task WebsitesTestTask = Task.Run(async() =>
            {
                try
                {
                    WebsitesTestWatch.Start();
                    await WebsitesTest.StartParallelAsync(websitesTestProgress, cts.Token);
                    WebsitesTestWatch.Stop();
                    WebsitesTestCheckmarkVisibility = Visibility.Visible;
                }
                catch (Exception ex)
                {
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        if (ex is OperationCanceledException || ex is WebException)
                        {
                            WebsitesTestProgressBarColour   = new SolidColorBrush(Colors.Red);
                            WebsitesTestCrossmarkVisibility = Visibility.Visible;
                            CancelBtnTag = null;
                        }
                    });
                }
            });

            await Task.WhenAll(ImageTestTask, NthNumberTestTask, WebsitesTestTask);

            //enable UI elements
            AsyncBtnIsEnabled   = ParallelAsyncBtnIsEnabled = ResetBtnIsEnabled = true;
            CancelBtnIsEnabled  = false;
            ParallelAsyncBtnTag = null;

            //Force update UI to enable disabled buttons
            CommandManager.InvalidateRequerySuggested();

            TotalTimeWatch.Stop();
            DTime.Stop();

            //Push the results of execution to the results manager
            ResultsDataManager.SetResults("Parallel + Async", ImageTestTimerText, PNumTestTimerText, WebsitesTestTimerText, TotalTimeText, !cts.IsCancellationRequested);
        }
Пример #3
0
        private void ParallelTest()
        {
            TotalTimeWatch.Start();

            //Initialize instances of Progress class that are later used to trigger
            //the events that update UI
            Progress <ImageTestReportModel> imageTestProgress = new Progress <ImageTestReportModel>();

            imageTestProgress.ProgressChanged += ImageTestProgressEvent;
            Progress <PNumTestReportModel> pNumTestProgress = new Progress <PNumTestReportModel>();

            pNumTestProgress.ProgressChanged += PNumTestProgressEvent;
            Progress <WebsitesTestReportModel> websitesTestProgress = new Progress <WebsitesTestReportModel>();

            websitesTestProgress.ProgressChanged += WebsitesTestProgressEvent;

            //Functions and variables in Image test are highly dependent on each other, therefore
            //I didn't find an efficient way to implement parallel execution for this test.
            Task ImageTestTask = Task.Run(() =>
            {
                ImageTestWatch.Start();
                ImageTest.StartSync(imageTestProgress);
                ImageTestWatch.Stop();
                ImageTestCheckmarkVisibility = Visibility.Visible;
            });

            Task PNumTestTask = Task.Run(() =>
            {
                PNumTestWatch.Start();
                PrimeNumbersTest.StartParallel(pNumTestProgress, pNumRangeTest, pNumNthTest);
                PNumTestWatch.Stop();
                PNumTestCheckmarkVisibility = Visibility.Visible;
            });

            Task WebsitesTestTask = Task.Run(() =>
            {
                try
                {
                    WebsitesTestWatch.Start();
                    WebsitesTest.StartParallel(websitesTestProgress);
                    WebsitesTestWatch.Stop();
                    WebsitesTestCheckmarkVisibility = Visibility.Visible;
                }

                catch (WebException)
                {
                    WebsitesTestProgressBarColour   = new SolidColorBrush(Colors.Red);
                    WebsitesTestCrossmarkVisibility = Visibility.Visible;
                }
            });

            //Wait until all tasks are finished. UI thread is blocked.
            Task.WaitAll(ImageTestTask, PNumTestTask, WebsitesTestTask);

            //Enable Reset button to let the user observe the results
            //and then reset UI before next run
            SyncBtnIsEnabled  = ParallelBtnIsEnabled = false;
            ResetBtnIsEnabled = true;

            //Stop Total timer and update all timers
            TotalTimeWatch.Stop();
            UpdateTimers(this, new EventArgs());

            //Push the results of execution to the results manager
            ResultsDataManager.SetResults("Parallel", ImageTestTimerText, PNumTestTimerText, WebsitesTestTimerText, TotalTimeText, true);
        }
Пример #4
0
        private async void AsyncTest()
        {
            //disable UI elements
            ResetUI();
            SyncBtnIsEnabled   = AsyncBtnIsEnabled = ParallelBtnIsEnabled = ParallelAsyncBtnIsEnabled = false;
            CancelBtnIsEnabled = true;
            AsyncBtnTag        = "Clicked";

            DTime.Start();
            TotalTimeWatch.Start();

            //Initialize instances of Progress class that are later used to trigger
            //the events that update UI
            Progress <ImageTestReportModel> imageTestProgress = new Progress <ImageTestReportModel>();

            imageTestProgress.ProgressChanged += ImageTestProgressEvent;
            Progress <PNumTestReportModel> pNumTestProgress = new Progress <PNumTestReportModel>();

            pNumTestProgress.ProgressChanged += PNumTestProgressEvent;
            Progress <WebsitesTestReportModel> websitesTestProgress = new Progress <WebsitesTestReportModel>();

            websitesTestProgress.ProgressChanged += WebsitesTestProgressEvent;


            //Use try catch statement for cancellation Tokens
            try
            {
                ImageTestWatch.Start();
                await ImageTest.StartAsync(imageTestProgress, cts.Token);

                ImageTestWatch.Stop();
                ImageTestCheckmarkVisibility = Visibility.Visible;
            }
            catch (OperationCanceledException)
            {
                ImageTestProgressBarColour   = new SolidColorBrush(Colors.Red);
                ImageTestCrossmarkVisibility = Visibility.Visible;
                CancelBtnTag = null;
            }

            try
            {
                PNumTestWatch.Start();
                await PrimeNumbersTest.StartAsync(pNumTestProgress, pNumRangeTest, pNumNthTest, cts.Token);

                PNumTestWatch.Stop();
                PNumTestCheckmarkVisibility = Visibility.Visible;
            }
            catch (OperationCanceledException)
            {
                PNumTestProgressBarColour   = new SolidColorBrush(Colors.Red);
                PNumTestCrossmarkVisibility = Visibility.Visible;
                CancelBtnTag = null;
            }

            try
            {
                WebsitesTestWatch.Start();
                await WebsitesTest.StartAsync(websitesTestProgress, cts.Token);

                WebsitesTestWatch.Stop();
                WebsitesTestCheckmarkVisibility = Visibility.Visible;
            }
            catch (Exception ex)
            {
                if (ex is OperationCanceledException || ex is WebException)
                {
                    WebsitesTestProgressBarColour   = new SolidColorBrush(Colors.Red);
                    WebsitesTestCrossmarkVisibility = Visibility.Visible;
                    CancelBtnTag = null;
                }
            }

            //enable UI elements
            AsyncBtnIsEnabled  = ParallelAsyncBtnIsEnabled = ResetBtnIsEnabled = true;
            CancelBtnIsEnabled = false;
            AsyncBtnTag        = null;

            //Force update UI to enable disabled buttons
            CommandManager.InvalidateRequerySuggested();

            TotalTimeWatch.Stop();
            DTime.Stop();

            //Push the results of execution to the results manager
            ResultsDataManager.SetResults("Asynchronous", ImageTestTimerText, PNumTestTimerText, WebsitesTestTimerText, TotalTimeText, !cts.IsCancellationRequested);
        }