Click here to Skip to main content
15,938,448 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How I Increase Progress bar while file copy what's wrong in that code?


C#
private void btn_start_Click(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync();
            
            
        }
        
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
          
            string oldPathAndName = @"F:\xp.flv";
            string newPathAndName = @"E:\New Movies\xp.flv";
            System.IO.File.Copy(oldPathAndName, newPathAndName);

        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            
            pr.Value = e.ProgressPercentage;

        }
Posted
Updated 26-Apr-14 23:45pm
v2
Comments
Member 13230900 2-Jun-17 4:55am    
i want to called one method which is in another class and that method is taking some path which is having some big files to parse in background. So the point is, my BW is not working... when i am calling my method(takePath) in DoWork function.

void m_Worker_DoWork(object sender, DoWorkEventArgs e)
		{
            // If the user clicks cancel the line
            // m_AsyncWorker.CancelAsync(); if ran above. This sets the 
            //CancellationPending to true.
            //You must check this flag in here and react to it. We react to it by setting 
            //e.Cancel to true and leaving
            textParseFile.takePath();
                }
this is short lines of my code....! it's showing error, there is no argument given correspods to required formal parameter 'sender' of textParseFile.takePath(obj, DOWorkEvents)

Simple: you aren't signalling any progress in your DoWork method! The system can't work out how far you have got, so it won't do it for you.
You report progress by calling from your DoWork method:
C#
backgroundWorker1.RepoertProgess(integerProgressValue);

In the case of the code you have there, you can't do it anyway, because a File.Copy operation is an blocking operation anyway - you call the method and it doesn't return until the copy is complete. So the progress would go from "0%" to "100%" immediately when the operation finished.

In this case, you would probably be better off using a "Marquee" style progress bar.
 
Share this answer
 
 
Share this answer
 
v2
Comments
Emre Ataseven 27-Apr-14 6:10am    
Seems like it doesn't work just for 1 file.
Mehdi Gholam 27-Apr-14 6:30am    
Like I said Copy() will blocking until finished, you can however read and write in blocks and call ReportProgress() yourself.
Mehdi Gholam 27-Apr-14 6:33am    
See the updated solution.
Rock (Multithreaded) 27-Apr-14 16:50pm    
 
Share this answer
 

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