• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5272
  • Last Modified:

Howto do bandwidth Throttling using C# .Net code

I am building a windows forms application using C# that will copy files to remote servers etc.  I want to implement bandwidth throttling from within my application and cant seem to find any examples.
Can you give me a good example that shows the .Net API's and Classes that are needed to implement throttling through my application.

I only have this posted as a 50 pointer because I have a feeling that Im going to be told that it cant be done using C# .Net and that I need to use a 3rd party app.

However, If I am getting a C# .Net solution I will up the points
1 Solution
what is bandwidth throttling?
stumpy1Author Commented:
You have a network pipe from 1 office to another, when an application is doing a copy or something over that pipe you dont want it using up all the available bandwidth.  You implement bandwidth throttling so that it only uses a specified percentage (or some other calculated amount) of the total bandwidth - thus all other applications that are working across that pipe can still function at the same time without too serious of a performance hit.
one option would be to run your process in another thread, and set the priority to low or lowest. that way, all other apps respond as if it wasn't running at all. i wrote a backup software program where i did that.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

stumpy1Author Commented:
Its nothing to do with task priority - Its bandwidth that I need throttled as opposed to the processor.
I need to put some code in the program so that it wont eat up all the network bandwidth while doing its copies, moves etc.
yes, i understood your problem... but if you were after making sure the other applications running were as responsive as possible, this would be an added step to attain that goal.
You can do this in C#, but you will have to copy blocks of data to a stream, and write your own logic to restrict the speed. I don't believe any built-in classes offer such.

Use something like:

        kbps = 100; // peak kilobytes per second to transfer - average will be a bit less
        StreamWriter sw = new StreamWriter(@"\\remote-server\folder", true, Encoding.UTF8, 1024);
        sw.AutoFlush = true;
        StreamReader sr = new StreamReader(@"c:\local.file");
        char[] buffer;
        int index;
        while(true) {          
          sr.ReadBlock(buffer, index, 1024); // read 1kb
          sw.Write(buffer);//write 1kb
          System.Threading.Thread.Sleep(1000/kbps); //appropriate delay to restrict throughput

Please note that code is a quick example and is not tested what-so-ever, but it is adaptable to achieve your solution.

You will either need a thread per transfer, or interleave the transfers in your own logic -- either will work fine.
stumpy1Author Commented:
Its not really what I had in mind but I like concept.  I tested out your idea but modified it so I could be used on all types of files, text, data, binaries, executables etc.  I probably wont end up using it - but I will file it for use at some time in the future.
This is the code: (replace file names).

                  int kbps = 100; // peak kilobytes per second to transfer - average will be a bit less
                  FileStream fs = new FileStream(@"\\remote-server\folder\filename", FileMode.Open, FileAccess.Read);
                  // Create the reader
                  BinaryReader r = new BinaryReader(fs);
                  FileStream fs2 = new FileStream(@"C:\folder\filename", FileMode.CreateNew);

                  // Create the writer for data.
                  BinaryWriter w = new BinaryWriter(fs2);
                  byte[] buffer;

                  for (int i = 0; i < fs.Length; i+=1024)
                        // Read data into buffer from input file.
                        buffer = r.ReadBytes(1024);
                        // Write data to ├▓utput file.
                        System.Threading.Thread.Sleep(1000/kbps); //appropriate delay to restrict throughput
stumpy1Author Commented:
Thanks for all the help
This project seems to be aimed at solving the exact problem you were working on:


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now