Products
Database Search Solution (New Version) Search Control SEO Pager Highlighter Shortcut Controls Crypt Package Free ASP.NET Controls
Geotargeting Component ASP.NET Media Player Control Flash Video Player Control Services
ASP.NET Telecommute Jobs Free IP Location Lookup Test .Net Regular Expressions CSS/Table/DIV Page Layouts Custom Programming Article Sites Master List |
Long Time Operations in ASP.NETAlmost every ASP.NET developer, before or later experiences a problem with page timeout error. This error occurs when execution of page takes long time and it is stopped by the server (default value is 90 seconds). Sometimes this error is result of simple mistake in source code, like never ending while loop. Also, common situation is that you need to optimize your SQL query or make your code more efficient, so it takes less time to execute. But, sometimes it is not your mistake at all, but you simply need more time to execute long running task from your ASP.NET web page. Timeout in Debug modeDefault script timeout on IIS is only 90 seconds. When you start web application in debug mode it is 300 000 seconds (almost a year). So, timeout errors are visible only when running release code. Change server timeout value with ASP.NET codeIf you need more time, simply change server parameter directly in code. Let say you expect that your script needs 5 minutes to execute: Server.ScriptTimeout = 300 As you see, you need to use ScriptTimeout property of Server object. You set wanted time in seconds. It is 32 bit integer, so the maximum time is Integer.MaxValue = 2147483647. I think it is more than 68 years, so it should be enough. :) If you need more time than this maybe you need to change your code because people don't want to wait that long for page to render. Obviously, there is not much sense to set negative value. When you change ScriptTimeout value, it is changed on application level and worth for every page on your site. Change server timeout in Web.configIf you want to have an option to change server timeout value later, after code is compiled, one of easy ways is to change it as parameter through a web.config. You can set server timeout with code like this: <httpRuntime executionTimeout="300"/> Don't forget, every time you change web.config file your web application will restart. In that case, it is good idea to use additional .config file for sensitive data or consider using a database. Informing web application users about long operationTimeout error is not your only problem. Web site visitors usually are not patience people. Some researches say that visitors leave a page if it is not loaded in eight seconds. If you have long running task, visitor could think that is some problem with web server or request is not sent to web server at all. Then he or she will try to refresh page, but that will not help... and you have confused and unhappy user. So, it is better to provide some explanation, a message to user, like simple "Please wait..." or even progress bar, so user knows everything is fine with web application "Please wait..." message, the old wayIf you use classic ASP or ASP.NET without code behind, you can do it on old way, with Response.Write method (code is in C#, but it is the same logic in VB.NET and VBScript): <% To use this, your visitors must have javascript enabled. Otherwise, your message will still be visible after long operation is finished "Please wait..." message with AjaxIf you use Ajax, you can provide better experience for you web application users. Instead of using Response.Write method, you can place a message to a label control and then call long operation through an asynchronous ajax request. Using threading for long running tasksYou can execute a long running task and avoid script timeout error if you simply increase ScriptTimeout value, but it is better solution if you execute your long task in separate thread. If you use new thread, you visitor don't need to wait until long operation is completed. Visitor can continue its work while long running task is executed in background. You can do that with code like this: First, import a System.Threading namespace. using System.Threading; Second, create a function which will perform long running task: private
void MyLongRunningTask() On some event, like button click, create a new thread and start a function: protected
void btnButton1_Click(object
sender, EventArgs e) Problem with application restart when running long tasks in ASP.NETAs you probably suppose, server timeout error is not your only problem when work with long running tasks. Application could restart because of different outside reasons, and then your long operation is stopped too. The reason can be change in global.asax file, change in machine.config, change in web.config, change of content of /bin folder, creating a /bin folder, and other various reasons, including some other application, like anti virus software. Because of that, it is possible that your long task can be stopped. You must have an answer for that situation. You can start the task again from the beginning, or better, you can save an information about current status somewhere, usually in database or .xml file. If your web application restarts because of some reason, long operation can be started again, read last checked point and take next step, instead of restarting complete operation. This is a must in some cases. For example, if you try to send a newsletter e-mails and your operation is aborted somewhere, it is not professional to restart complete operation, because some subscribers will get newsletter twice. Instead of that, you can save an information about sending progress. In case of accident, just read where the operation is stopped, and start from that point. Tutorial toolbar: Tell A Friend | Add to favorites | Feedback | Google |