The following answer assumes you're dealing with a situation in which:

1) You already have a window where you can show the results as a non- modal window 2) The process has a fixed number of 'units' to process (such as records in a database, bytes to d/l, etc...)

Here's my pseudo-code:

// Function prototypes
double Now(void); // this function returns the current time in microseconds.
void update(double total_time); // this function updates your window.

// a CONST for converting seconds -> microseconds.
const double conversion_factor = 1e6;

...

int i;
double time;
double delta_t;
double total_t; // total time remaining, NOT a countdown from now!

time = Now(); // seed our process estimate.
total_t = conversion_factor * 60.0 * 60.0; // 1 hr in us; a good guesstimate for d/l stuff.
update(total_t);
for (i = 1; i<=num_processing_units; ++i)
  {
  ... // do loop stuff.

  // Get the new delta_t...
  delta_t = (Now() - time);

// and adjust the estimate based on it, which is (time per 'unit' * # 'units' remaining to process).
  total_t = delta_t * ((double) num_processing_units - i);
  update(total_t);
  }
update(0.0);
...

Note that the update() function should take into account the granularity of your display units (i.e. if you have something like:

Time remaining: 1 h 23 m

then your granularity is minutes, so ignore any change smaller than a minute, so that you don't needlessly update the display if delta_t happens to be less than 1e6 * 60 (60,000,000 microseconds = 1 minute).

On Jun 1, 2008, at 7:50 AM, Nick Rogers wrote:

Hi,
I want to show time remaining while executing a loop.
How to go about it?

WIshes,
nick
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/wsquires% 40satx.rr.com

This email sent to [EMAIL PROTECTED]

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to