1

I need to implement Bandwidth Throttling feature in windows application. There are two threads on SO:

But that is for web app. I need it for windows app. How can I implement it in windows? Can I use above mentioned links for windows applications?

Here is the code I am using:

// Apply bandwidth control
int uploadLimit = GlobalClass.GetFileUploadLimit();

if (uploadLimit > 0)
{
  long bps = uploadLimit * 1024;
  const int BufferSize = 8192;
  MemoryStream mstream = new MemoryStream();//Stream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read, BufferSize);

  // Openup source stream.
  using (FileStream sourceStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read, BufferSize))
  {
    // Create throttled destination stream.
    ThrottledStream destinationStream = new ThrottledStream(mstream, bps);
    byte[] buffer = new byte[BufferSize];
    int readCount = sourceStream.Read(buffer, 0, BufferSize);

    while (readCount > 0)
    {
      destinationStream.Write(buffer, 0, readCount);
      readCount = sourceStream.Read(buffer, 0, BufferSize);
      client.FileUpload(Convert.ToInt16(userId), System.IO.Path.GetFileName(fileName), buffer);
      //Webservice: Here is the problem
    }
  }
}

In the above code, there is a web service I am using to upload file. This web service takes whole file in bytes at once. So in this case, I cannot upload file in chunks. Can anyone suggest me the some way to accomplish this or Shall I do change in service to accept data in chunks?

Community
  • 1
  • 1
Deepak Kumar
  • 672
  • 4
  • 15
  • 31

1 Answers1

1

Yes, You can use ThrottledStream in WinForms/WPF application.

AlfeG
  • 1,475
  • 4
  • 18
  • 33