Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
1.60/5 (2 votes)
See more:
Hi, i am copying a file from one location to another using File.copy(source, dest, bool). The problem i am facing is that the source file is of huge size(1.5 GB) and my User Interface(form) goes into a "not responding" state and resumes only when the copying gets completed.
How do i achieve it in a more efficient manner?

Please help!!!
Thanks :)
Posted

Apperantly someone has figured out a faster way of doing things:
A Faster File.Copy[^]

You might check it out.
 
Share this answer
 
Comments
LLLLGGGG 3-Jun-14 12:40pm    
Microsoft advices to use the static class File only with small files. With larger ones, Microsoft advices to use Streams
Kenneth Haugland 3-Jun-14 12:45pm    
Din't know that, guessed I learned something new today :)
LLLLGGGG 3-Jun-14 12:56pm    
This is because with the static class file, for example, if you have to read a part of a file, you have to cache the entire file in memory. With streams, this is not required as with streams you don't have to cache the entire file into memory.
Sergey Alexandrovich Kryukov 3-Jun-14 23:53pm    
Kenneth,

I checked this article and tried to test the code. I failed to see any distinct benefits of the method written by Frank. And it would be quite wonderful if it was the case. I also found some flaws in the way Frank gathered his evidence. Please see my comments:
#3,
#2,
#1.

—SA
Hi,

If you are using .net Framework 4.5 then use async-await. It parallel your task across multi core (if you have). It has same implementation as Task. The only big feature introduced in .net 4.5 is async-await. I will tell UI thread (Dispatcher Thread) that this process is long running and don't hang UI thread. execute this line of code in saperate thread (Task).

One of my friend (Solution 2 by Kumar Prateek) has provided you solution. but it is 40% done.

Basic syntax is


C#
private void async btnCopy_click(object sender, EventArg a)
{
   busyIndicator.IsBusy = true; //If any busy Indicator used.

   await CopyFile();

  busyIndicator.IsBusy=false; //turn of busy indicator
}


private Task CopyFile()
{
   return Task.Factory.StartNew(()=>{ /* Your copy implemenation*/});
}
 
Share this answer
 
Comments
sovit agarwal 4-Jun-14 1:31am    
Is it possible that i can, in every 1 min update the GUI with a message that the "copying is still taking place" and where should that code be written ?
sovit agarwal 4-Jun-14 3:40am    
What is busyIndicator here? a variable, method, event ?
Use a BackgroundWorker thread to perform the copy. You can even update the UI by reporting progress.

By the way, what other tasks are the user expected to do while the copying is active?
 
Share this answer
 
Comments
sovit agarwal 3-Jun-14 10:22am    
It wont be performing any other tasks but after the copying is done, the installation of that file will start on the machine...
Try making use of Task.Factory.StartNew method as shown bellow.

Task.Factory.StartNew(() => MethodToPerfomCopy()));


Here MethodToPerfomCopy method should contain your File.Copy(). If you do so a different process will be created and will not having any impact on the User Interface(UI).
 
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