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 // OutputCharLabels[i].Text = loResult.BrokenCode[i - loResult.FirstCharNumber].ToString(); } } }
private void DoWorkProcedure(object sender, DoWorkEventArgs e) { // This variable will hold the broken code string lsBrokenCode = ""; CodeBreakerParameters loCodeBreakerParameters = (CodeBreakerParameters)e.Argument; int liTotal = loCodeBreakerParameters.MaxUnicodeCharCode; Thread.Sleep(10000); // This code will break the simulated code. // This variable will hold a number to iterate from 1 to 65,535 - 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. TextBlock 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 = 0; liCharNumber < 4; //liCharNumber++) for (liCharNumber = loCodeBreakerParameters.FirstCharNumber; liCharNumber <= loCodeBreakerParameters.LastCharNumber; liCharNumber++) { loOutputCharCurrentLabel = OutputCharLabels[liCharNumber]; // This loop will run 65,536 times for (i = 0; i <= 65535; i++) { // if (bakCodebreaker.CancellationPending) if (((BackgroundWorker)sender).CancellationPending) { // The user requested to cancel the process e.Cancel = true; return; } // myChar holds a Unicode char lcChar = (char)(i); // loOutputCharCurrentLabel.Text = lcChar.ToString(); //Application.DoEvents(); // The percentage completed is calculated and stored in // the PercentageCompleted property loCodeBreakerProgress.PercentageCompleted = (int)((i * 100) / 65535); 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) // bakCodebreaker.ReportProgress(loCodeBreakerProgress.PercentageCompleted, loCodeBreakerProgress); ((BackgroundWorker)sender).ReportProgress(loCodeBreakerProgress. PercentageCompleted, loCodeBreakerProgress); // The old percentage completed is now the // percentage reported liOldPercentageCompleted = loCodeBreakerProgress. PercentageCompleted; } if (checkCodeChar(lcChar, liCharNumber)) { // The code position was found loCodeBreakerProgress.PercentageCompleted = 100; // bakCodebreaker.ReportProgress(loCodeBreakerProgress.PercentageCompleted, loCodeBreakerProgress); ((BackgroundWorker)sender).ReportProgress(loCodeBreakerProgress.PercentageCompleted, loCodeBreakerProgress); // The code position was found 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; } } // MessageBox.Show("The code has been decoded successfully.", this.Title); }