I try to use backgroundworker but it does not work - why?

This is an event code DoWorkevent ProgressChangedand event RunWorkerCompleted. The problem is that progressBar reaches 100% much faster than the functional process operation ends. Therefore, when the progressBar is up to 100%, I get an exception due to an error, since the progressBar cannot be 101%

I need to somehow make the progressBar achieve progress depending on the process / progress of the function. I think something is wrong with my calculations in the event DoWork.

At the top, Form1I added:

Int i;

In the constructor, I did:

i = 0;

backgroundWorker1.WorkerSupportsCancellation = true;
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.RunWorkerAsync();

And these are the events Backgroundworker:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
   BackgroundWorker worker = sender as BackgroundWorker;
   //int currentLength;
   //int currentIndex;
   //int lastIndex = 0;
   string startTag = "T256=\"";
   string endTag = "\"";
   int startTagWidth = startTag.Length;
   //int endTagWidth = endTag.Length;
   int index = 0;

   h = new StreamReader(@"d:\DeponiaSplit\testingdeponias_Translated.txt");

   while ((line = h.ReadLine()) != null)
   {
      if (index > f.LastIndexOf(startTag))
      {
         break;
      }

      int startTagIndex = f.IndexOf(startTag, index);
      int stringIndex = startTagIndex + startTagWidth;
      index = stringIndex;
      int endTagIndex = f.IndexOf(endTag, index);
      int stringLength = endTagIndex - stringIndex;

      if (stringLength != 0)
      {
         string test = f.Substring(stringIndex, stringLength);
         f = f.Substring(0, stringIndex) + line + f.Substring(stringIndex + stringLength);

         if (listBox1.InvokeRequired)
         {
            textBox1.Invoke(new MethodInvoker(delegate { textBox1.Text = line; }));
         }

         i = i + 1;
         System.Threading.Thread.Sleep(500);
         worker.ReportProgress((i * 1));
      }
   }

   h.Close();

   StreamWriter w = new StreamWriter(@"D:\New folder (24)\000004aa.xml");
   w.AutoFlush = true;
   w.Write(f);
   w.Close();
}

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    //this.progressBar1.Text = (e.ProgressPercentage.ToString() + "%");
    progressBar1.Value = e.ProgressPercentage;
}

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
   if ((e.Cancelled == true))
   {
      this.progressBar1.Text = "Canceled!";
   }
   else if (!(e.Error == null))
   {
      this.progressBar1.Text = ("Error: " + e.Error.Message);
   } 
   else
   {
       this.progressBar1.Text = "Done!";
   }
}

Why is it not working correctly?

Work with FileStream

progressBar , %, progressBar. progress progress progressBar % .

, ProgressChanged :

private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
    label3.Text = (e.ProgressPercentage.ToString() + "%");
} 

- label13 %, 1. - 1%... 2%... 3%... . ?

+3
2

File.ReadAllLines, . :

string[] lines = File.ReadAllLines(@"d:\DeponiaSplit\testingdeponias_Translated.txt"); 
// ...
worker.ReportProgress(i * 100 / lines.Length);
+4

ReportProgress() "i" , "i" "i + 1". - , , , . , , . , , . (i/totalNumLinesMatchingParameters) * 100. . . , , / ...

0

All Articles