Streaming without CPU occupying?

G

Guest

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top