private void RunWorkerCompletedProcedure(object sender, RunWorkerCompletedEventArgs e) { if (!e.Cancelled) { // Obtain the CodeBreakerResult instance contained in the Result property of e parameter CodeBreakerResult loResult = (CodeBreakerResult)e.Result; int i; // Iterate through the parts of the result resolved by this BackgroundWorker for (i = loResult.FirstCharNumber; i <= loResult.LastCharNumber; i++) { // The process has finishes, therefore the ProgressBar control must show a 100% prloProgressChar[i].Value = 100; // Show the part of the broken code in the label prloOutputCharLabels[i].Text = loResult.BrokenCode[i - loResult.FirstCharNumber].ToString(); } } }
private void DoWorkProcedure(object sender, DoWorkEventArgs e) { CodeBreakerParameters loCodeBreakerParameters = (CodeBreakerParameters)e.Argument; // This variable holds the last Unicode character to be processed //int liTotal = (int)e.Argument; int liTotal = loCodeBreakerParameters.MaxUnicodeCharCode; // This variable will hold the broken code string lsBrokenCode = ""; // This code will break the simulated code // This variable will hold a number to iterate from 0 to liTotal - Unicode character set int i; // This variable will hold a number to iterate from 0 to 3 (the characters positions in the code to be broken) int liCharNumber; // This variable will hold a char generated from the number in i char lcChar; // This variable will hold the current Label control that shows the char position being decoded //Label loOutputCharCurrentLabel; // This variable will hold a CodeBreakerProgress instance CodeBreakerProgress loCodeBreakerProgress = new CodeBreakerProgress(); // This variable will hold the last percentage of the iteration completed int liOldPercentageCompleted; liOldPercentageCompleted = 0; for (liCharNumber = loCodeBreakerParameters.FirstCharNumber; liCharNumber <= loCodeBreakerParameters.LastCharNumber; liCharNumber++) { // This loop will run (liTotal + 1) times for (i = 0; i <= liTotal; i++) { // We must check whether the user pressed the cancellation button or not if (((BackgroundWorker)sender).CancellationPending) { // The user requested to cancel the process e.Cancel = true; return; } // myChar holds a Unicode char lcChar = (char)(i); // The percentage completed is calculated and stored in the PercentageCompleted property loCodeBreakerProgress.PercentageCompleted = (int)((i * 100) / liTotal); loCodeBreakerProgress.CharNumber = liCharNumber; loCodeBreakerProgress.CharCode = i; if (loCodeBreakerProgress.PercentageCompleted > liOldPercentageCompleted) { // The progress is reported only when it changes with regard to the last one (liOldPercentageCompleted) ((BackgroundWorker)sender).ReportProgress(loCodeBreakerProgress.PercentageCompleted, loCodeBreakerProgress); //Application.DoEvents(); // The old percentage completed is now the percentage reported liOldPercentageCompleted = loCodeBreakerProgress.PercentageCompleted; } if (checkCodeChar(lcChar, liCharNumber)) { // The code position was found loCodeBreakerProgress.PercentageCompleted = 100; ((BackgroundWorker)sender).ReportProgress(loCodeBreakerProgress.PercentageCompleted, loCodeBreakerProgress); Application.DoEvents(); // The broken code is concatenated in lsBrokenCode lsBrokenCode += lcChar.ToString(); break; } } } // Create a new instance of the CodeBreakerResult class and set its properties values CodeBreakerResult loResult = new CodeBreakerResult(); loResult.FirstCharNumber = loCodeBreakerParameters.FirstCharNumber; loResult.LastCharNumber = loCodeBreakerParameters.LastCharNumber; loResult.BrokenCode = lsBrokenCode; // Return a CodeBreakerResult instance in the Result property e.Result = loResult; }