Exemplo n.º 1
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);
        }
Exemplo n.º 2
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);
        }