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

  • Home
  • SEARCH
  • 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 8415875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:34:28+00:00 2026-06-10T01:34:28+00:00

I have a background worker reporting the progress of the computation. It returns the

  • 0

I have a background worker reporting the progress of the computation. It returns the current line number it is processing in the ProgressChangedEventArgs. I can from there determine the percentage done by doing

(int) (100 * (double)e.ProgressPercentage / csTextLines) 

where csTextLines is the number of lines I am processing. The trouble I am running into is that I want to get a total estimated time and total time left also. There is nothing in background worker that I saw that would help me so I guess it has to be done with DateTime calculations. Here is what I have done so far to try and get TotalEstTime and TimeLeft

  • TimeLeft = TotalEstTime – TimeSoFar;
  • TotalEstTime = AvgTimeForEachLine * #OfLines;

Where

  • TimeSoFar = DateTime.Now – m_startTime;
  • AvgTimeForEachLine = (TimeFromLastLine + TotalTimeForAllLines) / #OfLines;

Where

  • TimeFromLastLine = DateTime.Now – LastRecordedTime;
  • TotalTimeForAllLines += TimeFromLastLine;

So if we turn this into code I have so far

// This event handler updates the progress. 
    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        TimeSpan difInCalls = DateTime.Now.Subtract(m_LastProgressCall);
        m_LastProgressCall = DateTime.Now;
        m_totalDif = m_totalDif.Add(difInCalls);
        TimeSpan avgDif = new TimeSpan(0, 0, 0, 0, ((int)m_totalDif.TotalMilliseconds / m_csNumLines));
        double totalTime = m_csNumLines * avgDif.TotalSeconds;
        double timeLeft = totalTime - (DateTime.Now.Subtract(m_Form2Start).TotalSeconds);
        Console.WriteLine("TotalEstTime: " + totalTime + " TimeLeft: " + timeLeft);


        // Update the progress label
        resultLabel.Text = "Line " + e.ProgressPercentage.ToString() + " of " + m_csNumLines + " at " + (int)(100 * (double)e.ProgressPercentage / m_csNumLines) + "% loaded";
    }

The problems that I am having are that my TotalTime grows significantly with how many lines are processed. In a 1437 line string it starts out at saying I have 1.4s and at the end it estimates 18.6s left. While actually taking 16.54s to complete.

My second problem is that the time left, does not really change. It fluctuates between about 1.1 and 2.5.

What could there be attributed to? What could be the problem?

Here is the full code: http://pastebin.com/x1CCceY7

  • 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-10T01:34:29+00:00Added an answer on June 10, 2026 at 1:34 am

    The problem is that you’re working off the last progress time, which will likely fluctuate as you go. If you work from the starting time and the current time, and use the percentage complete, then you’ll find that the time “self adjusts” as you move along.

    For example, if you store DateTime.Now into a variable (such as m_operationStart, using your naming), you could write:

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
      if (e.ProgressPercentage != 0)
      {
        double percentageComplete = (double)e.ProgressPercentage / m_csNumLines;
    
        TimeSpan timeSinceStart = DateTime.Now.Subtract(m_operationStart);
        TimeSpan totalTime = TimeSpan.FromMilliseconds(timeSinceStart.TotalMilliseconds / percentageComplete);
        TimeSpan timeLeft = totalTime - timeSinceStart;
    
        Console.WriteLine("TotalEstTime: " + totalTime + " TimeLeft: " + timeLeft);
    
    
        // Update the progress label
        resultLabel.Text = "Line " + e.ProgressPercentage.ToString() + " of " + m_csNumLines + " at " + (int)(100.0 * percentageComplete) + "% loaded";
      }
      else
        resultLabel.Text = "Line " + e.ProgressPercentage.ToString() + " of " + m_csNumLines;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that can spawn background worker threads from anywhere within it.
I have a background worker thread that is constantly syncing data to/from a remote
I have a background worker which can be cancelled. The normal flows interrupt itself
I have a background worker that performs loading of data from the database into
I have a background worker that runs 24/7 on a number of servers. I'd
I have a background worker which calls a function within a separate class. This
I have a background worker that stops after 100 iterations. Like this: BackgroundWorker bgWorker
lets say i have a background worker in a class that perform db query
Hi all I have a BAckground worker and a Datatable. I have a timer
Scenario I have a background worker in my application that runs off and does

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.