21

I have an aspx page which contains code that is liable to take 5 minutes or more to execute on the server. So I need to extend the amount of time before the Request times out.

What is the difference between putting this in the web.config for the site:

<location path="~/MyPage.aspx">
    <system.web>
      <httpRuntime executionTimeout="600"/>      
    </system.web>    
  </location>

and putting this in the code behind on the page:

protected void Page_Load(object sender, EventArgs e)
{
Page.Server.ScriptTimeout = 600;
}
Martin Smellworse
  • 1,702
  • 3
  • 28
  • 46

4 Answers4

15

As for the executionTimeout setting for ASP.NET's <httpRuntime> configuration not work problem. The documentation on this attribute is really not very clear. The problem is caused by the following reasons:

  1. This setting will take effect only when we set the "debug" to false in web.config , like:

when set to "debug=true" mode, the runtime will ignore the timeout setting.

  1. Even we set the debug="false", the executionTimeout will still has some delay when the value is very small. In fact, it is recommeded that we don't set the timeout less than 1.5 minutes. And when we set the timeout to less than 1 minute, the delay will span from 5 secs up to 15 secs. For example, if we set executionTimeout="5", it may take a bout 15 seconds for the page to timeout.

Server.ScriptTimeout property is a COM interface which is used in classic ASP. The executionTimeout of ASP.NET is the replacement of ScriptTimeout in asp.net , so we no longer need to use ScriptTimeout in asp.net.

In addition, as for have the script ALWAYS terminate after 2 seconds

I'm afraid there is no means in asp.net's runtime setting since the asp.net's runtime request processing management can't reach this level of accuracy, 2 seconds is a too small value which may make the performance very pool to monitor such a small interval. If we do need to let a certain processing timeout, we can consider put the timeout logic in the above application code level. For example, if we're executing SqlCommand , we can set the sqlcommand 's execution timeout. Or if we are executing a async call in page code, we can set a timeout for the async call

Hope helps.

Amarnath Balasubramanian
  • 9,300
  • 8
  • 34
  • 62
  • Balasubramani - So you're saying that Server.ScriptTimeout is 'a COM interface which is used in classic ASP'. So, in my asp.net web site, which is running on IIS 7.5 - setting Page.Server.ScriptTimeout is completely pointless? Is that absolutely the case? Other people who have answered suggest I can or should use Server.ScriptTimeout. – Martin Smellworse Jan 03 '14 at 11:19
  • @MartinSmellworse yes that is what i have mentioned and use execution timeout and i got this answer from Microsoft online support – Amarnath Balasubramanian Jan 03 '14 at 11:21
  • [How the Execution Timeout is managed in ASP.NET](https://learn.microsoft.com/en-us/archive/blogs/pedram/how-the-execution-timeout-is-managed-in-asp-net): Internally ASP.NET uses a Timer (an instance of System.Threading.Timer) to invoke the request cancelation process. This timer is fired once every **15 seconds**, so if the executionTimeout is set to 3 seconds, in reality the request can timeout at any time between 3 seconds and 18 seconds. – tibx Apr 19 '21 at 14:38
8

From this page, some official info: https://msdn.microsoft.com/en-us/library/system.web.httpserverutility.scripttimeout%28v=vs.110%29.aspx

The ScriptTimeout property can be set in the Web.config file by setting the executionTimeout attribute of the element. Setting the time-out programmatically with the ScriptTimeout property takes precedence over the Web.config setting.

I just thought it might be beneficial to note that Microsoft says "executionTimeout" is the same property as "ScriptTimeout".

FirstFraktal
  • 358
  • 4
  • 6
  • If you set the web.config setting and then check ScriptTimeout at run-time, you'll see that they are the same value as well. – Kris Nov 01 '16 at 18:42
4

executionTimeout

Optional TimeSpan attribute. Specifies the maximum number of seconds that a request is allowed to execute before being automatically shut down by ASP.NET. This time-out applies only if the debug attribute in the compilation element is False. To help to prevent shutting down the application while you are debugging, do not set this time-out to a large value. The default is "00:01:50" (110 seconds).

ScriptTimeout

A default ScriptTimeout can be set for a Web service or Web server by using the AspScriptTimeout metabase property. The ScriptTimeout property cannot be set to a value less than that specified in the metabase. For example, if NumSeconds is set to 10, and the metabase setting contains the default value of 90 seconds, scripts will time out after 90 seconds. However, if NumSeconds were set to 100, the scripts would time out after 100 seconds.

Nagaraj S
  • 13,316
  • 6
  • 32
  • 53
  • 1
    Thank you also for your answer. But I am still unclear - what is the difference between the two methods? Is one better or more suitable than the other? If so, in what circumstances? I simply have one page that I know will take many minutes to process what is asked of it. Which method should I use - set the executionTimeout just for that one page, in the web config for the site - or set Page.Server.ScriptTimeout on that individual page? – Martin Smellworse Jan 03 '14 at 10:36
  • I too am interested in the difference in these timeout values. Also, for long running scripts (5+ minutes) what settings in the application pool would need to be set? @MartinSmellworse – kstubs Sep 30 '14 at 17:48
0

Key difference is one is for an entire site and the other you can set just at a page level.

They are the same setting. Use the web.config for most things and if you need a longer execution for a specific page (that might run a long report or batch job) set this at the page level:

\\set page timeout to 40 minutes
Page.Server.ScriptTimeout = 2400;
Til
  • 5,150
  • 13
  • 26
  • 34
Formition
  • 11
  • 3
  • Quick pro tip for anyone using Azure Web Apps, Azure has a hard 240 second limit that you cannot work around. However, the application behind azure still obeys the limit. So the user gets disconnected but the code behind continues to run until the ScriptTimeout limit is reached. We were able to use this to overcome a long running page, when the user refreshed it was able to tell them, the job is still running and show them the progress so far. just use Application level variables. e.g. Application["IsPaymentJobRunning"] = True; if ((bool)Application["IsPaymentJobRunning"]) {} – Formition Mar 01 '19 at 04:18