Пример #1
0
        static void Main(string[] args)
        {
            OperationsTimer.DisplayTimerProperties();

            string s1 = "abcdefghijklmnopqrstuvwxyz";
            string s2 = "abcdefghijklmnapqrstuvwxyz";

            Stopwatch timer = Stopwatch.StartNew();

            if (perse.isDuplicated(s1))
            {
                Console.WriteLine("it has duplicated characters.");
            }
            else
            {
                Console.WriteLine("They are all unique characters.");
            }

            if (perse.isDuplicated(s2))
            {
                Console.WriteLine("it has duplicated characters.");
            }
            else
            {
                Console.WriteLine("They are all unique characters.");
            }

            timer.Stop();

            Console.WriteLine(timer.ElapsedTicks);

            timer = Stopwatch.StartNew();

            if (BruteForce.isDuplicated(s1))
            {
                Console.WriteLine("it has duplicated characters.");
            }
            else
            {
                Console.WriteLine("They are all unique characters.");
            }

            if (BruteForce.isDuplicated(s2))
            {
                Console.WriteLine("it has duplicated characters.");
            }
            else
            {
                Console.WriteLine("They are all unique characters.");
            }

            timer.Stop();
            Console.WriteLine(timer.ElapsedTicks);
        }
        //TODO: implement
        //- PROGRESS BAR: injecting new Progress<int>(p => pbDownloadProgress.Value = p)
        //- CANCELLATION ACTION: 'Convert' button changes a label("Cancel") and to CLICL is attached a RoutedEventHandler CancelConversion_Click() while TASK in progress
        private void BtnConvertImage_Click(object sender, RoutedEventArgs e)
        {
            string nano, mili;
            bool   isHighResolution;

            btnLoad.IsEnabled = false;

            OperationsTimer.StartMeasurement();

            var originalImage = (imgPhotoOrginal.Source as BitmapImage);

            try
            {
                if (cbAsync.IsChecked == false)
                {
                    imgPhotoConverted.Source = originalImage.ToBitmap().ToGrayscale(cts.Token).ToBitmapImage();
                }
                else
                {
                    cts.CancelAfter(TimeSpan.FromSeconds(1));
                    //BitmapImage result = await Task.Run(() => {
                    //    return originalImage.ToBitmap().ToGrayscale(cts.Token).ToBitmapImage();
                    //});
                    //imgPhotoConverted.Source = result;

                    //<!--!> BELOW is other 'working' implementation using ThreadPool in scenario when TASK(Promise) is returned from diff Thread/Context - it is less elegant(requires a delegation of following tasks into UI SynchronizationContext) but I think it is worth mentioning:

                    var sc = SynchronizationContext.Current;

                    ThreadPool.QueueUserWorkItem(async delegate
                    {
                        // work on ThreadPool
                        var cts            = new CancellationTokenSource();
                        BitmapImage result = await ImageProcessing.GreyscaleAsync(orginalImage, cts.Token);
                        result.Freeze();

                        sc.Post(delegate
                        {
                            // work on the original context (UI)
                            imgPhotoConverted.Source = result;
                            cts.Cancel();

                            (nano, mili, isHighResolution) = OperationsTimer.StopMeasurement();

                            lblPerformance.Content = String.Concat("Total time: ", String.Concat(nano, " ns | "), String.Concat(mili, " ms | "), String.Concat("HiRes: ", isHighResolution ? "Enabled" : "Disabled"));
                        }, null);
                    });