Timeout of an ASP.NET page

In some cases it can take quite some time for the web server to complete your webpage. Setting a time out is not that difficult but Googling around you will either find over complicated scenarios or a very simple and clear story in Dutch. Before recapitulating that one in English I have to credit the author Michiel van Otegem. Besides writing some good content he’s done a lot of work for the Dutch user group dotned. See him smiling here, he’s the guy on the final picture.


The timeout for a page request depends whether you are running debug or release code. The setting is in the web.config. In debug mode the timeout is 30000000 seconds which is about a year. In release mode the timeout is only 90 seconds. In some cases that may not be enough. You cannot set the timeout as a property of the page itself; it is a property of the global Server object. Set it in the page init event and reset it in the page unload event.



        private int timeOut;


        private void KopierenJaar_Init(object sender, System.EventArgs e)


        {


            timeOut = Server.ScriptTimeout;


            // Give it 1 hour = 3600 seconds


            Server.ScriptTimeout = 3600;


        }


 


        private void KopierenJaar_Unload(object sender, System.EventArgs e)


        {


            Server.ScriptTimeout = timeOut;


        }


In case your page is doing heavy things in the database you have to set db timeouts as well. See here for a quick look at that.

This entry was posted in ASP.NET. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • Reza

    You cannot use the Server.ScriptTimeout if “debug” attribute of Compilation element in web.config is set to “false”
    http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.scripttimeout(v=vs.110).aspx

  • David Alalibo

    You need to take note of the following to solve this:
    The shutdowntimeout property for the Application pool being used (go through IIS).
    The Scripttimeout property for the application.
    The Connecttimeout property to the database
    The Commandtimeout property for the connection
    The Asynch timeout property for the Ajax ScriptManager on the page (if you are using AJAX).
    The HTTPRunTIme executionTimeOut property in the web.config file
    Cheers.

  • David Alalibo

    You need to take note of the following to solve this:
    The shutdowntimeout property for the Application pool being used (go through IIS).
    The Scripttimeout property for the application.
    The Connecttimeout property to the database
    The Commandtimeout property for the connection
    The Asynch timeout property for the Ajax ScriptManager on the page (if you are using AJAX).
    The HTTPRunTIme executionTimeOut property in the web.config file
    Cheers.

  • http://zeeshanumardotnet.blogspot.com/ Zeeshan Umar

    Pvanooijen it is really useful for me thanks for sharing. I tried it and it is working fine.

  • http://codebetter.com/members/pvanooijen/default.aspx pvanooijen

    It does not work on your machine but does work elsewhere. Noteworthy, usualy the other way round :) Before blaming IE6, try what happens with another browser on your machine

  • Tung

    Yup I have tried to set the scripttimeout on page but it doesn’t fix the error.

    It seems like only my machine encounter this error so far, and I’m running on IE6

    the tester run the application on her machine yesterday and she did not encounter any error with the setting ‘Show HTTP friendly message’ on or off’

    I’m begining to wonder if only IE6 has this problem?

  • http://codebetter.com/members/pvanooijen/default.aspx pvanooijen

    Did you check if the timeout realy is 3600 ?
    Perhaps overriden by who knows what ?
    Does setting it from the page work ?

  • Tung

    Hi,

    I have a problem here hope you can advise me.

    I have set executionTimeout in web.config to 3600 (which I found out that the scripttimeout will follow this value), and in the production server, I have set debug=false, session time out = 20 min. sql command time out = 1200 seconds

    However, when the application is performing heavy sql task that exceed 90seconds, I got the ‘Page Could Not be Displayed’ error.

    When I change debug=true, the application works fine. The application also works fine when I turn off the ‘Show HTTP friendly message’.

    Any idea what am I missing here? the application works fine in non-debug mode in my local machine

    thanks in advance :)

  • http://codebetter.com/members/pvanooijen/default.aspx pvanooijen

    7400 secs ? That’s over 2 hours. Quite a coffe break. To test updating the setting while running I would set it to something like 5 seconds in the page int and update it it every 4 secs form your code. If you can keep your page alive it is possible , in case it times out it’s not possible.

    Quite often the exception being thrown is not of the type expected. Try by catching a basic Exception and inspect it’s actual type. In many cases I found that quite surprising.

  • Preetam

    Hi again,

    I tried calling Server.ScriptTimeout again and again in the code but I do not think it resets the timeout…..and I have set the time out to 7400 secs… Is that a valid timeout? and also wondering is there an exception that is thrown when a timeout occurs that can be handled??? MSDN says a HttpException is thrown and I trying to catch it in my code but that doesn’t seem to work??? any thoughts on this will be appreciated..

    Thanks :)

  • http://codebetter.com/members/pvanooijen/default.aspx pvanooijen

    At first thought I wouldn’t expect taht to happen. But it takes just a simple test to be sure :)

  • Preetam

    Hi,

    Just a quick question … say in my code I keep calling the Server.ScriptTimeout after a certain interval of time as the execution is on progress… will this refresh the script timeout???

    Not able to find an answer to this on the web.. please help.. thanks

  • HugoZ

    The changes to the Global.asax file are not being applied, what do we need to do in order to refresh the setting?

  • http://www.tradeready.net b2b

    what about @page directive?

  • http://petersgekko.codebetter.com pvanooijen
  • Surya Narayan Behera

    How to handle the Server Time out Error in asp.net with VB

  • http://petersgekko.codebetter.com pvanooijen
  • Maria

    Hi Geek,

    Can some pls help me in this problem ?

    My Web application has timeout set to 90 sec(which is default) and the DB commandtimeout changed to 90 sec (default is 30 sec) , if my application runs more than 90 sec i get the Timout error from DB, so i increased the DB timeout to 200 sec, now the query works fine, but what is the purpose of the web request timeout which is 90 sec ?

    Actally speaking when the request takes more than 90 sec it shd through me an error ? right ?

    Note : The application is publised and not in debuig mode.

  • http://programcsharp.com Chris Hynes

    The Server.Timeout property actually sets the (for some unbeknowst reason internal) HttpContext.Timeout property, so there is no reason to worry about threading, concurrency, or global change issues.

  • http://petersgekko.codebetter.com pvanooijen

    Why would an ashx be different ? After all it’s just another request longing for a response. You want to know for real ? Run a test !

  • Ranjit Singh

    Working Fine. But The Comments can not be ignorted for this article. can we do so with .ashx file which can work independent of rest of form. ?

    Please Let Me Know If yes.
    Thanks

  • http://www.ep-hi.net/blog/ Ben

    You should update your CSS for us firefox users!

    This does the trick for me:
    .CommentArrow {position:relative;top:2px}

  • vinay ashar

    Bingo!!!!!!!!!!!
    It works for me….
    thanx buddy

  • http://codebetter.com/blogs/peter.van.ooijen/ pvanooijen

    Thanks Gerald. Your experiment shows that Ayende’s fear is not justified.

  • Gerald

    I decided to check the validity of this by writing a test program containing two pages. In one of the pages I am setting the ScriptTimeout property to 3000, while in the other page the value of the property is not changed. It turns out that after loading the first page, then the second page, the value of the ScriptTimeout in the second page is reverted to its default value (90 seconds for release, 30000000 for debug) or the value set in the configuration files. In conclusion, the ScriptTimeout property, when changed in one page, does not change at a Global level.

  • http://codebetter.com/blogs/peter.van.ooijen/ pvanooijen

    As it’s always 90 secs everywhere imho it looks like debugging is disabled. How and where to (re-)set that is beyond my knowledge. I would start with your machine.config.

  • http://certificados.sytes.net/PopToWeb/Mail.aspx Helios

    Hello

    There is some problem with the Server.ScriptTimeout because, no matter what I do (on iis 6.0, on web.config, or in c# in many places of my code), every process stops exactly at 90secs, even more I defined in IIS 6.0 a 5000 secs timeout, and when within the code I ask for that value, it is 5000 as defined, but even so, the process stops at 90secs, and here is the incredible, no matter is if debug or release, where debug is suppoussed to stops in a year.

    Really I do not know what to do, and for the application I made it is mandatory to use a very high value.

    Helios

  • http://codebetter.com/blogs/peter.van.ooijen/ pvanooijen

    Global is bad, it would have been better to have a property on the page.
    The page unload will restore the value, so it’s not a permanent change.
    When you need a really long time it’s better to do it in a new thread. Which has other drawbacks. More on that in an upcoming post.

  • http://www.ayende.com/Blog/ Ayende Rahien

    Just be sure to remember that this page is _not_ the only one that is running on the page.
    And this mean that the same page running in parallel will very easily set the timeout _permenantly_ to whatever value you choose.
    Generally a bad thing.