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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek