Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy Multiple Files in a Thread

I've the following scenario, I've to copy multiple (about 10,50,200,...) files. I do that syncronously one after another. This is my code snippet for that.

static void Main(string[] args)
        {
            string path = @"";
            FileSystemWatcher listener = new FileSystemWatcher(path);
            listener.Created += new FileSystemEventHandler(listener_Created);
            listener.EnableRaisingEvents = true;

            while (Console.ReadLine() != "exit") ;
        }

        public static void listener_Created(object sender, FileSystemEventArgs e)
        {
            while (!IsFileReady(e.FullPath)) ;
            File.Copy(e.FullPath, @"D:\levani\FolderListenerTest\CopiedFilesFolder\" + e.Name);
        }

So when files are created in some folder and it's ready to use I copy that file one after another, but I need to start copying as soon as any file is ready to use. So I think I should use Threads. So.. How to implement parallel copying?

@Chris

Check if file is ready

public static bool IsFileReady(String sFilename)
        {
            // If the file can be opened for exclusive access it means that the file
            // is no longer locked by another process.
            try
            {
                using (FileStream inputStream = File.Open(sFilename, FileMode.Open, FileAccess.Read, FileShare.None))
                {
                    if (inputStream.Length > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }

                }
            }
            catch (Exception)
            {
                return false;
            }
        }
like image 214
levi Avatar asked Jun 14 '12 08:06

levi


People also ask

Can you copy multiple files at once?

Step 1: Click on the first file to be selected. Step 2: Hold down Ctrl and click on all the files that you want to select additionally. Step 2: Press the Shift key and click on the last file. Step 3: You have now selected all files at once and can copy and move them.

What is a multi-threaded copy?

Multi-threaded Copy Program (MCP)(ARC-16494-1) MCP is a high-performance file copy utility that achieves performance gains through parallelization. Multiple files and parts of single files are processed in parallel using multiple threads on multiple processors. The program employs the OpenMP and MPI programming models.

Is Robocopy multi-threaded?

One particular feature that makes Robocopy special (and often overlooked) is its multi-threaded feature that allows copying multiple files simultaneously. Instead of one file at a time using the copy feature built into File Explorer.


Video Answer


1 Answers

Doing parallel I/O from a mechanical disk is a bad idea and will only slow things down, as the mechanical head needs to spin every time to seek the next read location (a very slow process) and will then be bounced around as each thread gets its turn to run.

Stick to the sequential approach and read the files in a single thread.

like image 153
Tudor Avatar answered Oct 07 '22 18:10

Tudor