I implemented a System.Web.IHttpAsyncHandler
to limit website bandwidth usage for files download. Once defined user bandwidth, I need to send a byte[]
every second (or 1000 milliseconds, to be precise). Something like:
public class DownloadHandler : IHttpAsyncHandler
{
public IAsyncResult BeginProcessRequest(
HttpContext context, AsyncCallback cb, object extraData)
{
// user can download that file?
DownloadAsync download = new DownloadAsync(...);
download.StartDownload()
return download;
}
}
class DownloadAsync : IAsyncResult
{
// ...
public void StartDownload()
{
ThreadPool.QueueUserWorkItem(new WaitCallback(startAsyncTask));
}
private void startAsyncTask(object state)
{
// ...
while (context.Response.IsClientConnected &&
offset < data.Arquivo.Tamanho)
{
// ... do stuff
context.Response.OutputStream.Write(buffer, 0, readCount);
// ... more stuff
Thread.Sleep(1000 - (int)elapsed.TotalMilliseconds);
}
}
}
Once in ThreadPool.QueueUserWorkItem
, I lose control over frequency my code is being executed, so it don't take a second to execute, and that difference reflects on download throughput.
So, my questions are:
- Can I define
ThreadPool.QueueUserWorkItem
check interval? - If not, are there another approach to achieve this requirement (bandwidth throttling?)
- If not, can I have a pony?
TIA