Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7858061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:15:19+00:00 2026-06-02T21:15:19+00:00

This is the code of the DoWork event the ProgressChanged event and the RunWorkerCompleted

  • 0

This is the code of the DoWork event the ProgressChanged event and the RunWorkerCompleted event. The problem is that the progressBar is getting to 100% much faster before the function process operation is end. So when the progressBar is up to 100% I’m getting error exception since the progressBar can’t be 101%

I need somehow to make that the progressBar will get progress according to the function process / progress. I guess that something is wrong with my calculation in the DoWork event.

In the Form1 top I added:

Int i;

In the constructor I did:

i = 0;

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

And this is the events of the 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 doesn’t it work correctly?

Working using FileStream

Now I wanted to add to the progressBar a label which will show % and the label will move according to the progressBar I don’t want to disable the progressBar green color progress just to add % and show the numbers in percentages.

So in the ProgressChanged event I did :

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

But it doesn’t work – the label13 doesn’t move only the % changed to 1. I want to see something like 1%…2%…3%… and so on. How can I do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T21:15:21+00:00Added an answer on June 2, 2026 at 9:15 pm

    Why don’t you use File.ReadAllLines, it returns an array of strings. You could report your progress as the percentage of the line you are processing to the total number of lines:

    string[] lines = File.ReadAllLines(@"d:\DeponiaSplit\testingdeponias_Translated.txt"); 
    // ...
    worker.ReportProgress(i * 100 / lines.Length);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using a backgroundworker that can have n instances. Problem is the DoWork
I'm creating an array of BackgroundWorker that shares one event handler like this: BackgroundWorker[]
I need to change my backgroundworker progress outside the DoWork Event is this possible
I am getting this error in my event logs for a service I put
This code fails when I try to debug it using VC2010: char frd[32]=word-list.txt; FILE
This code builds without problems but fails when executed: var query = _db.STEWARDSHIP .OrderByDescending(r
This code: #include Backpack.h #include <sstream> #include <algorithm> int Backpack::getCurrentWeight() { int weight =
This code works in IDLE but not in the commandline. Why the difference? poem
this code is supposed to list recent calls with recent same nos skipped but
This code renders the motion of the player, changing the picture. Locally it works

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.