Beispiel #1
0
        private void DoWorkProcedure(object sender, DoWorkEventArgs e)
        {
            char                  character;
            TextBlock             currentTextBlock;
            CodeBreakerParameters breakerParameters = (CodeBreakerParameters)e.Argument;
            int characterPosition = breakerParameters.FirstCharNumber;

            int    maxCharCode = breakerParameters.MaxUnicodeCharCode;
            string brokenCode  = "";

            var breakerProgress          = new CodeBreakerProgress();
            int oldldPercentageCompleted = 0;

            currentTextBlock = _outputCharLabels[characterPosition];

            for (int i = 0; i <= 65535; i++)
            {
                Thread.SpinWait(40000);

                if (((BackgroundWorker)sender).CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }

                character = (char)(i);

                breakerProgress.PercentageCompleted = i * 100 / 65535;
                breakerProgress.CharNumber          = characterPosition;
                breakerProgress.CharCode            = i;

                if (breakerProgress.PercentageCompleted > oldldPercentageCompleted)
                {
                    ((BackgroundWorker)sender).ReportProgress(breakerProgress.PercentageCompleted, breakerProgress);
                    oldldPercentageCompleted = breakerProgress.PercentageCompleted;
                }

                if (CheckCodeChar(character, characterPosition))
                {
                    brokenCode += character.ToString();

                    breakerProgress.PercentageCompleted = 100;
                    ((BackgroundWorker)sender).ReportProgress(breakerProgress.PercentageCompleted, breakerProgress);
                    break;
                }
            }

            CodeBreakerResult breakerResult = new CodeBreakerResult
            {
                FirstCharNumber = breakerParameters.FirstCharNumber,
                LastCharNumber  = breakerParameters.LastCharNumber,
                BrokenCode      = brokenCode
            };

            e.Result = breakerResult; // it can be accessed in the completed event handler
        }
Beispiel #2
0
        private void BtnStart_Click(object sender, RoutedEventArgs e)
        {
            btnStart.IsEnabled = false;
            btnStop.IsEnabled  = true;

            CodeBreakerParameters parameters1 = new CodeBreakerParameters
            {
                MaxUnicodeCharCode = 32000,
                FirstCharNumber    = 0,
                LastCharNumber     = 0
            };

            CodeBreakerParameters parameters2 = new CodeBreakerParameters
            {
                MaxUnicodeCharCode = 32000,
                FirstCharNumber    = 1,
                LastCharNumber     = 1
            };

            CodeBreakerParameters parameters3 = new CodeBreakerParameters
            {
                MaxUnicodeCharCode = 32000,
                FirstCharNumber    = 2,
                LastCharNumber     = 2
            };

            CodeBreakerParameters parameters4 = new CodeBreakerParameters
            {
                MaxUnicodeCharCode = 32000,
                FirstCharNumber    = 3,
                LastCharNumber     = 3
            };

            worker1.RunWorkerAsync(parameters1);
            worker2.RunWorkerAsync(parameters2);
            worker3.RunWorkerAsync(parameters3);
            worker4.RunWorkerAsync(parameters4);
        }