コード例 #1
0
        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (e.UserState == null)
            {
                if (e.ProgressPercentage >= 0)
                {
                    this.statProgressBar.Increment(10);// = e.ProgressPercentage;
                }
                else
                {
                    this.Cursor                = Cursors.AppStarting;
                    this.statGeneral.Text      = "Processing";
                    this.rtbStringResults.Text = string.Empty;
                    this.btnCancel.Enabled     = true;
                    this.btnProcess.Enabled    = false;
                    this.btnExpand.Enabled     = false;

                    this.statProgressBar.Minimum = 0;
                    this.statProgressBar.Maximum = 100;
                    this.statProgressBar.Value   = 0;

                    Regex numFind = new Regex(@"\d{1,9}");
                    this.statLinesProcessed.Text = numFind.Replace(this.statLinesProcessed.Text, "0");
                    this.statEmptyLines.Text     = numFind.Replace(this.statEmptyLines.Text, "0");
                    this.statMissingValues.Text  = numFind.Replace(this.statMissingValues.Text, "0");
                    this.statLinesSubmitted.Text = numFind.Replace(this.statLinesSubmitted.Text, "0");
                }
            }
            else if (e.UserState is BulkLinePurge)
            {
                Regex         numFind = new Regex(@"\d{1,9}");
                BulkLinePurge purge   = e.UserState as BulkLinePurge;
                this.rtbStringResults.Text  += purge.LineResults;
                this.statLinesProcessed.Text = numFind.Replace(this.statLinesProcessed.Text, purge.LinesProcessed.ToString());
                this.statEmptyLines.Text     = numFind.Replace(this.statEmptyLines.Text, purge.ExcludedEmptyLineCount.ToString());
                this.statMissingValues.Text  = numFind.Replace(this.statMissingValues.Text, purge.ExcludedMissingValueLineCount.ToString());
                this.statLinesSubmitted.Text = numFind.Replace(this.statLinesSubmitted.Text, purge.TotalLinesSubmitted.ToString());
            }
        }
コード例 #2
0
        /// <summary>
        /// The true worker method
        /// </summary>
        /// <param name="data">StringData object that contains the items to process</param>
        /// <param name="worker">BackgroundWorker object used for threading</param>
        /// <param name="e">DoWorkEventArgs used for threading</param>
        /// <returns>Returns total lines processed</returns>
        private int ProcessStringFormatting(StringData data, BackgroundWorker worker, DoWorkEventArgs e)
        {
            worker.ReportProgress(-1);

            int emptyLinesExcluded        = 0;
            int missingValueLinesExcluded = 0;

            char[] delimiter = Utility.GetDelimiter(data.Delimiter);

            StringBuilder sb             = new StringBuilder();
            int           linesProcessed = 0;
            int           totalLines     = 0;
            int           length         = data.Lines.Length;

            totalLines = length;

            //Get the count of format elements in the string
            Regex           formatCounter        = new Regex(@"\{[0-9]+\}");
            MatchCollection matches              = formatCounter.Matches(data.Format);
            int             maxMatchFormatNumber = 0;
            int             minMatchFormatNumber = int.MaxValue;
            int             tmp;

            for (int i = 0; i < matches.Count; i++)
            {
                Int32.TryParse(matches[i].Value.Replace("{", "").Replace("}", ""), out tmp);
                if (tmp + 1 > maxMatchFormatNumber)
                {
                    maxMatchFormatNumber = tmp + 1;
                }
                if (tmp < minMatchFormatNumber)
                {
                    minMatchFormatNumber = tmp;
                }
            }

            int tenPercent = Convert.ToInt32(length * 0.1);

            for (int i = 0; i < length; i++)
            {
                //If excluding empty lines, see if there is a blank line or a line of delimiters with no values.
                if (data.ExcludeEmptyLines && data.Lines[i].Replace(delimiter[0].ToString(), "").Trim().Length == 0)
                {
                    emptyLinesExcluded++;
                    continue;
                }

                //Get a string array of the values to process.
                string[] vals = data.Lines[i].Trim().Split(delimiter);

                //Also consider it empty if is doesn't at least contain the lowest format number
                if (data.ExcludeEmptyLines && vals.Length - 1 < minMatchFormatNumber)
                {
                    emptyLinesExcluded++;
                    continue;
                }

                bool skip = false;
                //If supposed to skip missing values, see if there are any missing values
                if (data.ExcludeMissingValues)
                {
                    //If the array contains less then the needed format lengths, it's automatically a skip
                    if (vals.Length < maxMatchFormatNumber)
                    {
                        missingValueLinesExcluded++;
                        skip = true;
                    }
                    else
                    {
                        //Otherwise, check each value to see if it's blank.
                        for (int j = 0; j < maxMatchFormatNumber; j++)
                        {
                            if (vals[j].Trim().Length == 0)
                            {
                                missingValueLinesExcluded++;
                                skip = true;
                                continue;
                            }
                        }
                    }
                }
                else //If we are to include lines with missing values, insert the default fill text
                {
                    //If the array isn't long enough, make it bigger.
                    if (vals.Length - 1 < maxMatchFormatNumber)
                    {
                        string[] tmpArr = new string[maxMatchFormatNumber];
                        tmpArr.Initialize();
                        vals.CopyTo(tmpArr, 0);
                        vals = tmpArr;
                    }

                    //Check the values and add default as needed
                    for (int j = 0; j < vals.Length; j++)
                    {
                        if (vals[j] == null || vals[j].Trim().Length == 0)
                        {
                            vals[j] = data.DefaultFillText;
                        }
                    }
                }

                if (skip)
                {
                    continue;
                }

                if (data.TrimInputValues)
                {
                    for (int j = 0; j < vals.Length; j++)
                    {
                        vals[j] = vals[j].Trim();
                    }
                }

                try
                {
                    sb.Append(String.Format(data.Format, vals));
                    if (!data.RemoveCarrigeReturns)
                    {
                        sb.Append("\r\n");
                    }

                    linesProcessed++;
                    if (worker.CancellationPending)
                    {
                        e.Cancel = true;
                        BulkLinePurge purge = new BulkLinePurge(sb.ToString(), linesProcessed, emptyLinesExcluded, missingValueLinesExcluded, totalLines);
                        worker.ReportProgress(0, purge);
                        sb.Length = 0;
                        return(linesProcessed);
                    }
                    if (tenPercent > 0 && linesProcessed % tenPercent == 0)
                    {
                        worker.ReportProgress(linesProcessed);
                    }
                }
                catch (System.FormatException)
                {
                    if (!data.ExcludeMissingValues)
                    {
                        BulkLinePurge purge = new BulkLinePurge(sb.ToString(), i, emptyLinesExcluded, missingValueLinesExcluded, totalLines);
                        worker.ReportProgress(0, purge);
                        sb.Length = 0;
                        if (DialogResult.No == MessageBox.Show("Line " + Convert.ToString(i + 1) + " does not contain enough values. Skip line and continue?", "Missing Values", MessageBoxButtons.YesNo, MessageBoxIcon.Warning))
                        {
                            break;
                        }
                    }
                    else
                    {
                        missingValueLinesExcluded++;
                    }
                }
            }

            BulkLinePurge finalPurge = new BulkLinePurge(sb.ToString(), linesProcessed, emptyLinesExcluded, missingValueLinesExcluded, totalLines);

            worker.ReportProgress(0, finalPurge);
            e.Result  = finalPurge;
            sb.Length = 0;
            return(linesProcessed);
        }