ASP.NET
  Home arrow ASP.NET arrow Page 3 - ASP.NET Basics, Part 4: Looping the Loop
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

ASP.NET Basics, Part 4: Looping the Loop
By: Harish Kamath (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 15
    2003-10-20

    Table of Contents:
  • ASP.NET Basics, Part 4: Looping the Loop
  • Counting Down
  • The Infinite Loop and the Careless Coder
  • Dos and Don'ts
  • For-gone Conclusion
  • The Sound of Breaking Loops
  • End of Play

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    ASP.NET Basics, Part 4: Looping the Loop - The Infinite Loop and the Careless Coder


    (Page 3 of 7 )

    A word of caution to novice programmers (and careless experienced onesalso): if you fail to provide a valid condition to exit your loop, youwill get stuck in an infinite loop, one of the worst nightmares for adeveloper.

    The likely symptom of this condition in the ASP.NET context is a serverthat fails to respond for an extended period of time while executing thescript. In such a situation, you can rest assured that an infinite loopis the most likely culprit. Don't take my word for it, try it foryourself (note that the following code deliberately introduces aninfinite loop for illustrative purposes, so use it at your own risk):


    <script language="C#" runat="server">
    void Page_Load()
    {  
           
            
    // define a loop counter
            
    int countdown10;
           
            
    output.Text "Beginning countdown...";
           
            
    // repeat
            
    while(countdown >= 0)
           {
                    
    output.Text += "
    countdown;
                  
    // don't change the loop counter, thereby introducing
                  // an infinite loop
                    // countdown--;
            
    }
           
            
    output.Text += "<h1>Houston, we have lift-off!</h1>";
           
    }      
    </script>
    <html>
    <head><title>Countdown To Launch</title></head>
    <body>
    <asp:label id="output" runat="server" />
    </body>
    </html>



    This example deliberately "forgets" to alter the value of the loopcounter every time the loop runs. As a result, the conditional testalways ends up true and the script never exits the "while" loop. Hence,the Web server continues to execute the script (and eat memory) tillsuch time as you forcibly terminate the script by shutting down theserver, or the server times out.

    Since Web servers can't rely on a human being around 24/7 to monitor theserver and kill rogue scripts, most of them include built-in safeguardsto ensure that such rogue scripts do not completely take over systemresources. They do this by including a timeout period, which specifiesthe maximum amount of time given to a particular script to completeexecution. If the script exceeds this time limit, the Web server willautomatically halt further execution of the script and return an error.For the IIS server, you can modify this value using the Internet ServiceManager; read more about this athttp://www.microsoft.com/windows2000/en/server/iis/htm/core/iipy_32.htm.

    Note, however, that just because the Web server has this built-intimeout capability, it is not an excuse for you, the developer, to writesloppy code; until the server timeout is activated, other requests willbe queued unnecessarily, affecting the performance of the server andannoying your users. Therefore, it is important to always ensure thatyour loop has a valid exit condition...because prevention is better thancure!

    More ASP.NET Articles
    More By Harish Kamath (c) Melonfire


     

    ASP.NET ARTICLES

    - Developing a Mini ASP.NET AJAX Server Centri...
    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT