🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #9779

Re: CopyFileEx - CopyProgressRoutine - devision by zero

#9779
From: Bob Gonder
Date: Tue, 15 Jun 2004 18:08
66 lines
3701 bytes
Tommy wrote:

>1. completly size of all bytes which need to be copy (size)
>2. size of already copied bytes (already_copied)
>3. percent of already copied size (CGauge2->Progress)
>
>QueryPerformanceCounter(&start_ticks);
>QueryPerformanceFrequency(&frequenz);
>dir_create(ESource->Text,ETarget->Text);
>
>Now I have a Timer in my applcation which clocks echt second.
>
>void __fastcall TForm1::Timer1Timer(TObject *Sender)
>{
>   rest = size-already_copied;
>   QueryPerformanceCounter(&ende_ticks);
>   tick_diff = ((double) ende_ticks.QuadPart - (double)
>start_ticks.QuadPart)  frequenz.QuadPart;
>   if (already_copied > 0)

>   LRestdauer->Caption = int(double((double)rest/(double)already_copied) *
>(double)tick_diff) + " s";

TIP: When writing math fomulas with a lot of casting, it's often
prudent to add a coment showing the math without all the fluff.
So:
	(Remaining / Copied) * Elapsed
Yep, that's the math, all right.
But, you can do it with integers if you rearange things:
	(Remaining * Elapsed) / Copied
Added benefit, if Elpased is in 1/1000's second, RemainingTime will be
also. So you can define your precision by how precise ElapsedTime is.


>Any Idea why this to not works. Is there perhabs a better way to get the
>seconds which are need till the process is finished?

Way too complicated.
Aren't you using CopyFileEx() ?  The Callback happens fairly often
(more than once per second on my system), so no Timer needed.
And the Performance Counter isn't needed either. You aren't doing
multimedia or guideance systems.  System Time is accurate enough for
this job.

 Use GetSystemTimeAsFileTime()
The 64bit result is 10 million per second, so divide by 10 million to
get seconds:

	GetSystemTimeAsFileTime( &ftNow );
	ElapsedSeconds = ( ftNow - ftStart ) / 10000000uint64;
You have to do some fiddling to get the FILETIMEs into UINT64s so you
can subtract them

Now, I wanted to use FILETIME to figure out how many
hours:minutes:seconds were left, so I didn't divide Elapsed by 10
million. But, it doesn't take too many Gigs on a slow connection to
overflow when you are multiplying by 10 million per second.
    Talking Gigs * 10millions, it will take a total of
    4Gigs * 400(10millions) to overflow the uint64.
To avoid Overflow, I ballparked a figure of 10,000 which gives me
400Gigs remaining after 40,000 seconds, and still leaves me with
elapsed acuracy of 1/1000 second (important in the early stages of the
copy)
	(  (Remaining * (Elapsed/10000)) / Copied  )*10000
	FileTimeToSystemTime( &ftRemaining, &SystemTime );
	Display SystemTime as Hours:Minutes:Seconds
Technical Details
Message-ID: <8hauc01e3dsrlu787m5s8jo10rpu0k3duq@4ax.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <40ce2e93@newsgroups.borland.com> <crtsc0pp05ki34cqsvkgsj721eei6g7obh@4ax.com> <40cef067$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,9a3709a4832c0e51
X-Google-Attributes: gid10cc67,public
From: Bob Gonder <notbg@notmindspring.invalid>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: CopyFileEx - CopyProgressRoutine - devision by zero
Date: Tue, 15 Jun 2004 11:08:47 -0700
Organization: MindSpring Enterprises, Inc.
Reply-To: notbg@notmindspring.invalid
Message-ID: <8hauc01e3dsrlu787m5s8jo10rpu0k3duq@4ax.com>
References: <40ce2e93@newsgroups.borland.com> <crtsc0pp05ki34cqsvkgsj721eei6g7obh@4ax.com> <40cef067$1@newsgroups.borland.com>
X-Newsreader: Forte Agent 1.93/32.576 English (American)
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 65.173.66.53
X-Trace: newsgroups.borland.com 1087322926 65.173.66.53 (15 Jun 2004 11:08:46 -0700)
Lines: 67
Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail