Exemplo n.º 1
0
        private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            Service.SADataService saData = new Service.SADataService();
            //Because this object exists on the main thread I have to create it here on the backgroundworker thread
            TextBox txtBoxOutput = new TextBox();

            // txtBoxOutput.Text = saData.ConvertText(@"c:\data\pg02.txt");
            //saData = null;

            //add all files in the directory to the array
            string textfield;

            //const string V = @"\";

            //textfield = Path.GetFullPath(this.txtBoxFilePath.Text).Replace("\\", V);
            textfield = Path.GetFullPath(this.txtBoxFilePath.Text);
            var arrayFilesAndPath = Directory.EnumerateFiles(textfield, "*.txt").ToList();

            //string[] arrayFilesAndPath = Directory.GetFiles(@"C:\", "*.txt");
            if (arrayFilesAndPath.Count != 0)
            {
                //for every file found import/convert into MS Access Database
                for (var i = 0; i < arrayFilesAndPath.Count; i++)
                {
                    //Calculate percentage complete
                    if (i == 0)
                    {
                        //Convert integer to float
                        float f = (((float)i / (float)arrayFilesAndPath.Count) * 100f);
                        progress = (int)f;
                        backgroundWorker1.ReportProgress((int)progress);
                    }

                    //Check if there is a request to cancel the process
                    if (backgroundWorker1.CancellationPending)
                    {
                        e.Cancel = true;
                        backgroundWorker1.ReportProgress(progressBar1.Value);
                        return;
                    }

                    /* Output the File and Path */
                    txtBoxOutput.Text += arrayFilesAndPath[i];

                    if (txtBoxOutput.TextLength != 0)
                    {
                        backgroundWorker1.ReportProgress(progress, txtBoxOutput);
                    }

                    /* Begin Conversion */
                    //txtBoxOutput.Text = saData.ConvertText(arrayFilesAndPath[i]);
                    saData.ConvertText(arrayFilesAndPath[i], data => backgroundWorker1.ReportProgress(progress, data));
                }

                saData = null;

                //If the process exits the loop, ensure that progress is set to 100%
                //Remember in the loop we set i < 100 so in theory the process will complete at 99%
                SADataService.DataInsertion.CloseConnection();
                txtBoxOutput.Text = "Copying data to database complete";
                backgroundWorker1.ReportProgress(100, txtBoxOutput);
            }
            else
            {
                txtBoxOutput.Text = "Text Files not found in specified path";
                backgroundWorker1.ReportProgress(100, txtBoxOutput);
            }
        }
Exemplo n.º 2
0
        public void DoProcessing(IProgress <SpecialStructs.progressData> progress)
        {
            Service.SADataService saData = new Service.SADataService();
            //Because this object exists on the main thread I have to create it here on the backgroundworker thread

            // txtBoxOutput.Text = saData.ConvertText(@"c:\data\pg02.txt");
            //saData = null;

            //add all files in the directory to the array
            string textfield;


            //const string V = @"\";

            //textfield = Path.GetFullPath(this.txtBoxFilePath.Text).Replace("\\", V);
            textfield = Path.GetFullPath(this.txtBoxFilePath.Text);
            var arrayFilesAndPath = Directory.EnumerateFiles(textfield, "*.txt").ToList();

            //string[] arrayFilesAndPath = Directory.GetFiles(@"C:\", "*.txt");
            if (arrayFilesAndPath.Count != 0)
            {
                //for every file found import/convert into MS Access Database
                for (var i = 0; i < arrayFilesAndPath.Count; i++)
                {
                    //Calculate percentage complete
                    if (i == 0)
                    {
                        //Convert integer to float
                        //float f = (((float) i / (float) arrayFilesAndPath.Count) * 100f);
                        //progress = (int) f;
                        //backgroundWorker1.ReportProgress((int) progress);
                    }

                    //Check if there is a request to cancel the process
                    if (ThreadGlobals.ShouldCancel)
                    {
                        progress.Report(new SpecialStructs.progressData
                        {
                            UseFileProgressBar = true, progressValue = i + 1, TotalValue = arrayFilesAndPath.Count,
                            UserData           = "Canceled Process"
                        });
                        //e.Cancel = true;
                        //backgroundWorker1.ReportProgress(progressBar1.Value);
                        this.Invoke(new MethodInvoker(delegate
                        {
                            this.btnReadFiles.Enabled = true;
                            this.lblStatus.Text       = "Import Processing Canceled";
                        }));

                        // Close the database connection
                        SADataService.DataInsertion.CloseConnection();

                        return;
                    }

                    /* Begin Conversion */
                    //txtBoxOutput.Text = saData.ConvertText(arrayFilesAndPath[i]);
                    //Report progress of changing Files
                    progress.Report(new SpecialStructs.progressData
                    {
                        UseFileProgressBar = true, progressValue = i + 1, TotalValue = arrayFilesAndPath.Count,
                        UserData           = arrayFilesAndPath[i]
                    });
                    //convert file
                    saData.ConvertText(arrayFilesAndPath[i], data => progress.Report(data));
                }

                saData = null;

                //If the process exits the loop, ensure that progress is set to 100%
                //Remember in the loop we set i < 100 so in theory the process will complete at 99%
                progress.Report(new SpecialStructs.progressData
                {
                    progressValue = 100, TotalValue = 100, UserData = "Copying data to database complete"
                });
                SADataService.DataInsertion.CloseConnection();
                //txtBoxOutput.Text = "Copying data to database complete";
                //backgroundWorker1.ReportProgress(100, txtBoxOutput);

                this.Invoke(new MethodInvoker(delegate
                {
                    this.btnReadFiles.Enabled = true;
                    this.lblStatus.Text       = "Import Processing Completed";
                }));
            }
            else
            {
                progress.Report(new SpecialStructs.progressData
                {
                    progressValue = 100, TotalValue = 100, UserData = "Text Files not found in specified path"
                });

                //txtBoxOutput.Text = "Text Files not found in specified path";
                //backgroundWorker1.ReportProgress(100, txtBoxOutput);
                this.Invoke(new MethodInvoker(delegate
                {
                    this.btnReadFiles.Enabled = true;
                }));
            }
        }