Streaming without CPU occupying?

Discussion in 'ASP .Net' started by =?Utf-8?B?dGhlIGZyaWVuZGx5IGRpc3BsYXkgbmFtZQ==?=, Dec 17, 2005.

  1. 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?
    =?Utf-8?B?dGhlIGZyaWVuZGx5IGRpc3BsYXkgbmFtZQ==?=, Dec 17, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. A.M

    Delay without blocking CPU

    A.M, Jun 11, 2004, in forum: ASP .Net
    Replies:
    10
    Views:
    582
  2. Alessandro Giambruno

    Waiting until timer without cpu crash?

    Alessandro Giambruno, Jun 18, 2004, in forum: Java
    Replies:
    9
    Views:
    2,883
    Roedy Green
    Jun 22, 2004
  3. Philip Parker
    Replies:
    11
    Views:
    2,711
    Rufus V. Smith
    Jul 7, 2004
  4. Sunfire
    Replies:
    7
    Views:
    430
    Sunfire
    Nov 10, 2007
  5. pavunkumar

    How , system cpu and user cpu times calculates

    pavunkumar, Feb 27, 2009, in forum: C Programming
    Replies:
    1
    Views:
    337
Loading...

Share This Page