Example #1
0
        // This event handler updates the progress.
        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            //Trace.WriteLine("progresschanged"+ e.ProgressPercentage);
            ProgressReporter pr = (ProgressReporter)e.UserState;

            if (pr.GetTotalStaff() > 0)
            {
                this.progressBar1.Maximum = pr.GetTotalStaff() + 1;
            }
            textBox1.Text           = pr.GetResult();
            label1.Text             = pr.GetStatus();
            this.progressBar1.Value = e.ProgressPercentage;
        }
Example #2
0
        // This event handler is where the time-consuming work is done.
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            //Trace.WriteLine("asyncwork "+ (string)e.Argument);
            BackgroundWorker worker = sender as BackgroundWorker;

            ProgressReporter progressReporter = new ProgressReporter(STATUS_LOADING_CSV);

            worker.ReportProgress(0, progressReporter);

            //creates an instance of the C++ class Class1, which contains the find duplicates code
            ClassLibrary.Class1 instance = new ClassLibrary.Class1();

            if (worker.CancellationPending == true)
            {
                //Trace.WriteLine("Cancelation pending");
                e.Cancel = true;
            }
            //calls the c++ functions for reading, parsing and finding duplicates, and
            //processes the return values to update the UI in C#, then calls again, etc,
            //until its completed or cancelled
            else if (instance.ReadCSVFile((string)e.Argument))
            {
                //successfully read csv file

                if (worker.CancellationPending == true)
                {
                    //Trace.WriteLine("canceled3");
                    e.Cancel = true;
                }
                else
                {
                    //Trace.WriteLine("success reading csv file");
                    progressReporter.SetStatus(STATUS_PARSING_CSV);
                    worker.ReportProgress(0, progressReporter);

                    int totalStaff = instance.ParseCSV();
                    //Trace.WriteLine("total staff:" + totalStaff);
                    progressReporter.SetTotalStaff(totalStaff);
                    progressReporter.SetStatus(STATUS_FINDING_DUPLICATES_1 + totalStaff + STATUS_FINDING_DUPLICATES_2 + STATUS_FINDING_DUPLICATES_3);
                    worker.ReportProgress(1, progressReporter);

                    bool     completed = false;
                    string[] result;
                    while (!completed)
                    {
                        if (worker.CancellationPending == true)
                        {
                            //Trace.WriteLine("canceled");
                            e.Cancel = true;
                            break;
                        }

                        /*gets a list of duplicate staff from c++ layer and splits it in 3 strings:
                         * the first contains the status ('1' == completed and '0' == in progress), for internal use by the code only
                         * the second is the number of staffs already checked, for internal use by the code only,
                         * and the third is the list to be shown to the user*/
                        result = instance.GetDuplicates().Split(new string[] { "\n" }, 3, StringSplitOptions.None);
                        progressReporter.UpdateResult(result[RESULT_INDEX_TO_USER]);
                        if (Int32.Parse(result[RESULT_INDEX_STATUS]) == 1)
                        {
                            //completed the whole processing, leaves the loop and prints the
                            //final output to the user via backgroundWorker1_RunWorkerCompleted
                            completed = true;
                            //Trace.WriteLine("completed");
                        }
                        else
                        {
                            //still in progress, updates the ui to the user
                            int progress = Int32.Parse(result[RESULT_INDEX_CURRENT_STAFF]) + 2;
                            worker.ReportProgress(progress, progressReporter);
                            //Trace.WriteLine("progress:" + progress);
                        }
                    }
                    e.Result = progressReporter.GetResult();
                    //Trace.WriteLine("Final result:" + e.Result);
                    if (worker.CancellationPending == true)
                    {
                        //Trace.WriteLine("canceled4");
                        e.Cancel = true;
                    }
                    else
                    {
                        //the processing is completed and the user didnt cancel it, so we write the final result into file
                        instance.WriteFinalResult();
                    }
                }
            }
            else
            {
                //failed to read csv file
                //Trace.WriteLine("failed to read csv file");
                if (worker.CancellationPending == true)
                {
                    //Trace.WriteLine("canceled2");
                    e.Cancel = true;
                }
                else
                {
                    //TODO handle fail
                    //Trace.WriteLine(STATUS_FAILED_OPEN);
                    e.Result = STATUS_FAILED_OPEN;
                }
            }

            //releases c++ resources
            instance.releaseAllResources();
        }