Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my question is how can we get progress percentage of acting a method.
for e.g when we are copying a file how can we get the amount of copied file in a variable,
or
when our application size is too big, and it takes a while to load it's needed files, how can we understand how much of our application files are uploaded?
suppose that the application has 3 100mb files that are needed to load when our application is executed. what is the way to understand how much of file sizes are uploaded and how much is remained?
you know, exactly I want to have a progress bar control in my first form that shows how much of files are uploaded and when all of them is uploaded completely, it shows a message that application is ready to use.

am I clear? if I explain my problem not so good, please tell me.
Posted
Comments
[no name] 16-Aug-12 14:14pm    
Your question seems to be "how do I calculate a percentage"?
Mostafa M.A 16-Aug-12 14:33pm    
not at all, the percentage is not important at all
the size of files that are copied is wanted...
Kschuler 16-Aug-12 14:23pm    
Can you explain in more detail what exactly you are doing? Are you talking about copying files while you are deploying a program? Or is your program doing the copying? Do you want to display the percentage of the count of files or of the actual bytes of files? Maybe if you gave us more info about how you are doing the copying we could help better.
Mostafa M.A 16-Aug-12 14:31pm    
let me explain more with an example:
the application needs two database files to run, each of them is 50mb.
when app wants to be executed it must copy database files in it's directory and then open them, then the application is ready to use.
I want to know the size of copied file in each moment
for e.g (now [23mb] out of 100mb is copied one second later [29mb] out of 100mb is copied and so on...
I want to know this size: 23mb -29mb .....

There is no single (or simple) way of knowing, what is the completion ratio for any operation especially if you want to display the completion progress based on the time the operation is going to last. Each operation is affected by different kinds of factors.

For example, copying a file is affected by following factors:
- source drive type and speed
- target drive type and speed
- connection speed to the drives
- fragmentation of the file
- location of the fragments, are they for example on the outer or inner part of the disk
- and so on and so on

One way to tackle this is to define for example a simple static class which always reports the progress of an operation by using an event. Each operation doing the actual work can report it's progress to this class.

The progress of the operations can be 'best guesses', for example you can split an operation to slices based on the estimated length of a slice. For example ome operation could be sliced like 10%+10%+40%+30%+10% if there's something in the middle which is expected to last longer. This way you can display a rough estimate, how the operation is progressing.

To calculate the slices, you must investigate the actual operations to get the estimate.

Hopefully that made sense.
 
Share this answer
 
Comments
Mostafa M.A 16-Aug-12 14:35pm    
and how I can write such a static class that you said?
I use the copy method in File Class, how can I write a class that can monitor the progress of copying method?
Wendelius 16-Aug-12 14:50pm    
You can't. If you're mostly interested in the time that the file copy takes, copy the file in pieces. But if you have several files then a quick solution could be that you calculate the total sice of all files to be copied and the progress is the sum of copied files (their bytes) versus the total amount of bytes to be copied.
Mostafa M.A 16-Aug-12 15:08pm    
ok I want the thing that you name (sum of copied files in bytes).
how can I get it?
Wendelius 16-Aug-12 15:49pm    
using FileInfo.Length Property [^] first, sum the lengths of all files to be copied and in the loop where you copy the file, accumulate the total bytes of copied file based on the file's length.

For example if yu have files with sizes like
- 1 MB
- 2 MB
- 1 MB
the total would be 4 and when you copy the files in a loop you would have the progress like:
- 1/4
- 3/4
- 4/4
Mostafa M.A 16-Aug-12 15:54pm    
it's a good answer, I like it. but please read the solution that I wrote myself below,and tell me your idea about it, I think it's more general. that means, exactly this idea you said now, popped in my mind several minutes ago, but more general.
Accurately? You can't. Sorry to disappoint, but think about it: Take some 50 mixed sized files (some big, some small) and copy them from one folder on your computer to another using Windows Explorer. You will get a box which tells you the remaining time. Is it accurate? Does it change and go up? Or down? Or down, then up, then down again, then up a lot, then stop, then drop to "15 seconds remaining" for an hour and half?

Microsoft has been trying to get this right since about the eighties...there is even an XKCD cartoon that is relevant: http://xkcd.com/612/[^]

If they can';t get it right, what chance do you have? Do what we all do: guess and use fudge factors!
 
Share this answer
 
Comments
Mostafa M.A 16-Aug-12 14:38pm    
you mean we can have the percentage of a copy progress NEVER?
i mean when 10mb out of 100mb is copied how can we get that (for e.g 10mb) amount? it seems you said it's not possible at all
OriginalGriff 16-Aug-12 14:51pm    
Not really, no. They are "atomic" processes - you start them and either wait for them to finish or go off and do something else until they tell you they are done (or you keep looking to see if they are). There is no built in mechanism for "I've done 10%!" or "Another 10Mb copied!" to be reported.
And worse, you can't accurately guess how long it's going to take either as it will vary computer to computer, load to load, and even depending on which anti-virus is loaded.
Mostafa M.A 16-Aug-12 15:12pm    
I get the point, but there is a question here
there are a lot of applications that when you run them at the splash screen or the main form the show something like this(the file that has been uploaded or the file has been copied or etc)
what is the number that they show?
there is a simple example for what I said
when you open one of Microsoft Office applications such as Word
in it's splash screen a number is shown of amount of it's progress,so what's that number?
OriginalGriff 17-Aug-12 2:39am    
Made up as it goes along.
It has a number of tasks to do - load this, load that - and it has a rough guess as to how long each will take on the developers machine. So he reports progress at specific points, or just uses a timer to report "progress" as a comfort thing. The later is more common that you might think... a lot of "progress" indicators are just run from a time and do not reflect the internal working of the app at all!
Sergey Alexandrovich Kryukov 16-Aug-12 16:47pm    
Right, a 5.
--SA
I notice that this question is tagged as c#, .Net 4.0. This means that you have the option of implementing your own assembly loader - which allows you to implement a pretty cool solution to the applicaton loading problem.

Loading Assemblies in Separate Directories Into a New AppDomain[^]
CLR Hosting APIs[^]

Best regards
Espen Harlinn
 
Share this answer
 
Comments
Wendelius 16-Aug-12 17:09pm    
Very good advice!
Espen Harlinn 16-Aug-12 17:21pm    
Thank you, Mika :-D
Abhinav S 16-Aug-12 23:21pm    
Correct. 5.
Espen Harlinn 17-Aug-12 4:08am    
Thank you, Abhinav :-D
StianSandberg 17-Aug-12 3:10am    
5'ed
% remaining = ((total value - current value) / total value) * 100. You will have to able to extend this to the problem you are trying to solve.

Oh and have a look at BackgroundWorker. It probably has what you need.
 
Share this answer
 
I have a solution for this, but I don't know exactly that it will really works right or not?
and it's just for getting copy method progress:
suppose that we have a directory named: Main, and it contains 3 files (1.zip, 2.zip, 3.zip)

[1].we can get this folder size by adding it's files sizes up.

and the destination folder would be named like this (Dest)
we start copying the files to Dest folder by File.copy method
we have a timer that ticks at every millisecond and each time it ticks a method runs named [Monitoring_copy_Progress].

what would this method do?
[2].every time it's called, it gets the Main and Dest folders sizes and returned them in 2 ref variables.

so in every millisecond we have the Dest folder size that is equal to the copied files amount and Main folder size that is the remained files size
also we got all files size before

so now we have everything ready for a progress bar

Is it a good solution for this?
Does it work right?
 
Share this answer
 
Comments
[no name] 16-Aug-12 15:56pm    
It seems like a good solution.
Mostafa M.A 16-Aug-12 16:00pm    
you think it will works right?
would it be any error or exception in this algorithm?
Wendelius 16-Aug-12 16:16pm    
If I recall correctly, this wouldn't help. The reason is that copying the file 'allocates' the blocks from the disk in the beginning of the operation. So what happens is that immediately when the copying starts, you would see the size of the file as it will be when the copying ends and you will not see the intermediate situation. So the result seen by the user is still the same as I described with the loop.
Mostafa M.A 16-Aug-12 16:21pm    
what you said above just shows us the number of copied files, it means that when a file copied completely it says us that one file out of all copied but what if we have just one file?
can we get the size of copied?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900