2

Hi I am using multithread to copy many files from a source to multi-network destinations, each thread copy a bulk of files to Different network! I use .net File.Copy(...) I see 100% uses on only one network, each moment

enter image description here

the 100% change from network to network.

  1. I tried to change the destinations to local one, then i see balanced bytes copy over all threads

  2. I tried to run 10 processes(each one to a different destination) instead of 10 thread, then I get all 10 network at 100% use.

I use .net 4.5 any idea ?

ilse2005
  • 11,189
  • 5
  • 51
  • 75
  • 1
    Can You provide any code ? – icbytes Mar 21 '16 at 12:48
  • its really just File.Copy , the destination is network one( \\10.161.1.1\...) – user5287071 Mar 21 '16 at 12:50
  • 1
    You surely seem to have buggy coding in spawning Your threads, as You mention in the beginning, the 100 usage is only per one network at a time. – icbytes Mar 21 '16 at 13:00
  • no bug, when i use local destinations the write bytes are balanced between all threads , its only happening when the destination is of a network..... – user5287071 Mar 21 '16 at 13:11
  • File.Copy is IO bound operation so your threads are just waiting for IO to complete operation. I'd suggest you to try async/await for IO bound operations and you'll see clear difference. – Saleem Mar 21 '16 at 14:08
  • 1
    There is no intrinsic limit. We need to see the code to see what IO patterns exactly you are causing. **Since you are repeatedly refusing to provide the code I'm closing the question.** It is not possible to help you under these circumstances. – usr Mar 21 '16 at 14:46
  • wow, someone rejected everyone's answer :) unbelievable. – Saleem Mar 21 '16 at 15:30
  • @Saleem I did, because the answers are complete guesswork and some of them outright wrong. The question is "why is my code (not shown) not working?". The "answers" are providing generic copy sample code that is wrong and guidance that does not apply. This question is such a train wreck. The correct approach is to first diagnose the problem and then fix it. – usr Mar 21 '16 at 20:04
  • Also, I did *not* downvote all answers outright. I downvoted them individually because easy is very objectionable. It just happens to be the case that all are bad. – usr Mar 21 '16 at 20:10
  • @usr you are right. We had limited insight of problem domain. All we tried to convey that what OP is mentioning may not be a problem and suggested what and where s/he should look. Obviously we can't ask for full dump to see what's going on. – Saleem Mar 21 '16 at 20:22
  • Right, every SO answerer will learn at some point to take a distance from unsolvable questions like this. This must be closed because it is an open end discussion instead of an addressable problem. We do know, though, that his issue is very specific from the symptoms. It's just not diagnosable. – usr Mar 21 '16 at 20:26
  • @usr I agree. It's at your discretion to close it or not. – Saleem Mar 21 '16 at 20:29

3 Answers3

0

I'd suggest you to replace threads (good for CPU bound operations) with async/await model which perform excellent for IO bound operations.

Let's rewrite File.Copy operation as

public static async Task Copy(string src, string dest)
{
    await Task.Run(() => {
        System.IO.File.Copy(src, dest);
    });
}

You can call it from calling method as snippet below.

var srcPath = "your source location";
var dstPath = "your destination location";

foreach(var file in System.IO.Directory.EnumerateFiles(srcPath))
{
    var dstFile = System.IO.Path.Combine(dstPath, System.IO.Path.GetFileName (file));
    Copy (file, dstFile);
}

Now you can simply pump this method with src and destination paths as fast as you can. Your limitation will be IO speed (disk/network etc) but your Cpu will be mostly free.

Saleem
  • 8,728
  • 2
  • 20
  • 34
-1

Have you taken at look at asynchronous operations. There is some really good documentations on MSDN about specifically async IO.

There are also some questions about async IO on stack overflow such as this one.

Using Task.Run you can queue all of your file copy operations asynchronously.

Community
  • 1
  • 1
Louis
  • 593
  • 4
  • 13
-1

Try something like:

    List<String> fileList = new List<string>();
    fileList.Add("TextFile1.txt");
    fileList.Add("TextFile2.txt");

    Parallel.For(0, fileList.Count, x =>
    {
        File.Copy(fileList[x], @"C:\" + fileList[x]);
    }
    );

Change c:\ to match your multiple destinations. If you provide the orignal code we could do more.

Nigel Findlater
  • 1,684
  • 14
  • 34