For Programmers: Free Programming Magazines  


Home > Archive > ASP .NET > December 2005 > Streaming without CPU occupying?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Streaming without CPU occupying?
the friendly display name

2005-12-17, 7:57 am

Following problem:

I want to stream a file to the client, but the logic takes too much CPU time
(100% in fact..) this is the code:



public class Download : System.Web.UI.Page

{

private byte[] buffer;
private AsyncCallback callback;
private Filestream fs;



private void Page_Load (object sender, System.Eventargs e)

{

fs = File.Open (....) // I won't bore you with the non interesting parts

Response.Clear();

Response.ClearContent();

Response.ClearHeaders();

Response.AddHeader("content-disposition", "attachment;
filename=blabla.exe");

Response.ContentType = "application/octet-stream";

fs.BeginRead(buffer, 0, 1024, callback, null);



}



void whencomplete (IAsyncResult Result)

{

int BytesRead = fs.EndRead(Result);

if (BytesRead > 0)
{

Response.BinaryWrite(buffer);

Response.Flush();

fs.BeginRead(buffer, 0, 1024, callback, null);

}

if (BytesRead == 0)
{

fs.Close();

Response.Close();

}



in private void InitializeComponent():

{

buffer = new byte[1024];

callback = new AsyncCallback (whencomplete);

}





---

This works, but it does take the whole CPU. If I add a sleep cycle in
whencomplete:

---


if (BytesRead > 0)
{

Response.BinaryWrite(buffer);

Response.Flush();
Thread.Sleep(100);

fs.BeginRead(buffer, 0, 1024, callback, null);

}
----

The CPU Utilization goes nearly to zero, but only with one running download.
I tried 3 downloads at the same time, and the CPU was again quite occupied.

What's the more effective way to stream? Is it even possible to stream,
without a high cpu utilization, since it's basicaly a .aspx site, which is
running in a loop?

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com