in

DavidJBerman.com

An online community of advocacy and support of entrepreneurial ventures

C sharp (C#)

August 2007 - Posts

  • How to create asynchronous ASP.NET pages using C#

    IIS combined with ASP.NET provides many technologies to improve performance and scalability.  IIS provides a pool of threads so that it can server many requests simultaneously.  The pool has a limited number of threads in it, and once they are used up additional requests can start to pile up.  Keeping the total number of active threads down is an attempt to prevent too many active threads from consuming all of the available CPU time.  However, with today's data intensive websites, much of the time threads are tied up waiting for an external resource such as a request from a web service or from a database.  Asynchronous pages in ASP.NET can boost performance in these situations by enabling threads in the pool to be used to serve additional requests while an operation is waiting for an external resource request to complete.

    Suppose you have a website with two web pages.  One is your home page which display's a greeting, and the second page displays a large dataset from a database.  You have 25 threads in your thread pool.  25 people simultaneously are accessing the database query page, and one additional person comes onto the site to see the home page which has static content on it.

     

    Compare these two scenarios:

    Synchronous database driven page:  While everyone is waiting for the dataset to load, all available threads are in use so the 26th request for the home page becomes blocked.

    Asynchronous database driven page:  While 25 requests are waiting for data from the database, those threads are returned to the pool for work.  When the 26th request comes in for the home page, that page is returned immediately.  As the datasets are returned the threads are drawn out of the thread pool to finish serving the pages.  The result is that the threads spend much more time available to serve requests.

     

    Here are two good pages for getting started with asynchronous ASP.NET pages:

     

    Asynchronous ASP.NET Page Processing by Peter Bromberg

    http://www.eggheadcafe.com/articles/20060918.asp

     

    Use Threads and Build Asynchronous Handlers in Your Server-Side Web Code by Fritz Onion

    http://msdn.microsoft.com/msdnmag/issues/03/06/threading/default.aspx

     

    After reading these and some other articles, here is a piece of code to get you started.  I put this together to process a job in the background as a generic pattern.  I wrap my big job in a delegate so that I can get an IAsyncResult object back.  This simplifies things, because in most examples I read you get this by calling a web service asynchronously but this isn't always what you want done.

     

    Step 1:  Make an empty asp.net page.  Add async="true" to the @Page tag in the .aspx file.

    Step 2:  In the class code, declare a delegate

                  public delegate void AsyncTaskDelegate();

    Step 3:  Declare a member variable in the class to prevent the delegate from going out of scope

     AsyncTaskDelegate _runnerDelegate = null;

    Step 4:  Create a method that will be run asynchronously:

                 public void DoJob()

                {

                     this.GridView1.DataSource = GetDatasetFromDatabase();        this.GridView1.DataBind();

               }

    Step 5:  Tell the framework you want your job run.  You can put this in Page_Load or in a response to a button click / postback:

           // Register async methods

            AddOnPreRenderCompleteAsync(

                new BeginEventHandler(OnBegin),

                new EndEventHandler(OnEnd)

            );

    Step 6:  Add the event to kick off the delegate and run the job asynchronously.

    IAsyncResult OnBegin(object sender, EventArgs e, AsyncCallback cb, object state)

          {

            _runnerDelegate = new AsyncTaskDelegate(this.DoJob);

            IAsyncResult result = _runnerDelegate.BeginInvoke(cb, state);

     

            return result;

          }

     

    Step 7:   Add an event handler for after the request finishes

    void OnEnd(IAsyncResult ar)

          {

            _runnerDelegate.EndInvoke(ar);

          }

     

     

    All together, it looks like this:

     

    public partial class Async : System.Web.UI.Page

    {

        public delegate void AsyncTaskDelegate();

       

        AsyncTaskDelegate _runnerDelegate = null;

     

        IAsyncResult OnBegin(object sender, EventArgs e,

    AsyncCallback cb, object state)

        {

            _runnerDelegate = new AsyncTaskDelegate(this.DoJob);

            IAsyncResult result = _runnerDelegate.BeginInvoke(cb, state);

     

            return result;

        }

     

        public void DoJob()

        {

      this.GridView1.DataSource = new AsyncTaskDelegate(this.DoJob);          

      this.GridView1.DataBind();

        }

     

        void OnEnd(IAsyncResult ar)

        {

            _runnerDelegate.EndInvoke(ar);

        }

     

        protected void Button1_Click(object sender, EventArgs e)

        {

            // Register async methods

            AddOnPreRenderCompleteAsync(

                new BeginEventHandler(OnBegin),

                new EndEventHandler(OnEnd)

            );

        }

    }

  • How to create a console window for a Windows Forms application

    Suppose you are writing a GUI application for Windows and you want to be able to debug your applicaiton by sending console output to a console window that opens only if you pass a certain command line argument or are compiled in debug mode.  Console.WriteLine sends your output to nowhere very fast.  If you want to have a console window, here is how you do it:

     

    Inside one of your classes insert this reference to the AllocConsole method in kernel32.dll:

    [DllImport("kernel32.dll")]
    public static extern Int32 AllocConsole();

    Now just call MyClass.AllocConsole() if you want a console for output and voila, there you go!  All output to Console now appears in your console window.

     

     

    Posted Aug 06 2007, 03:38 PM by Dave with no comments
    Filed under:
  • How to access command line arguments

    In a C# Windows Application or Console Application, you may want to access arguments passed to your application via the Command Line.

     This is actually very easy to do.  All you have to do is modify your Main() method to take in an array of strings:

     static void Main(string[] args) {
       foreach(string arg in args)
       {
           Console.WriteLine(String.Format("Arg: {0}", arg);
       }
      }
    Posted Aug 06 2007, 03:32 PM by Dave with no comments
    Filed under:
More Posts
Copyright 2006 David J. Berman, all rights reserved.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems