locked
Custom Request timeout issue? RRS feed

  • Question

  • User1040660288 posted

    I basically want to start a timer at the beginning of a request, and if it is takes longer than X, throw an exception, catch it, display a nice message to the user, and move on.  I have almost got what i want working with the following code: 

    'Delegate
    Dim timerDelegate As TimerCallback = AddressOf TimeLimiter

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Try
    Dim
    timer As New Threading.Timer(timerDelegate, Nothing, 5000, System.Threading.Timeout.Infinite)

    RunBigCalculation()

    Catch ex As Exception
    'Display nice message
    End Try
    End Sub

    Private Sub
    TimeLimiter(ByVal stateInfo As Object)
    Throw New Exception("Load taking too long")
    End Sub
    The problem i am having with the above is that I get an "unhandled error" exception.  The Try/Catch block never catches it. I think this is essentially a threading question. 
    If anyone can shed any light on what i am doing wrong, and how i can fix it, that would be great. 
    Wednesday, December 31, 2008 2:21 PM

Answers

  • User1616291716 posted

    Hi jshallard,

    It seems that you want to send a friendly message to the user when the request is TimeOut, If yes, I suggest you try to achieve it by refering to the following code:

     

    Imports System 
    Imports System.Collections.Generic 
    Imports System.Linq 
    Imports System.Text 
    Imports System.Web 
    
    Public Class MyHttpModule 
        Implements IHttpModule 
        Public Sub Init(ByVal ap As HttpApplication) 
            AddHandler ap.PreSendRequestHeaders, AddressOf ap_PreSendRequestHeaders 
        End Sub 
        
        
        Private Sub ap_PreSendRequestHeaders(ByVal sender As Object, ByVal e As EventArgs) 
            Dim hr As HttpResponse = DirectCast(sender, HttpApplication).Context.Response 
            If hr.StatusCode = 408 Then ' The request time out
                hr.Redirect("TimeOut.htm") 
            End If 
        End Sub 
        Public Sub Dispose() 
        End Sub 
    End Class 
    
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 4, 2009 9:23 PM

All replies

  • User2104673743 posted

    Whe you run a timer, the timer lives on a different thread, so throwing an exception on a different thread wont be caught by the catch method you have.

    I could suggest that you adjust this page scriptTimeOut property so the page will stop executing (do this before calling the "RunBigCalculation" method and catch the exception just as you are doing now.

    Hope this helps.

    Friday, January 2, 2009 10:13 AM
  • User1040660288 posted

    Hey jportelas - thanks for your response.

    For a couple of different reasons I would like to find another way (lack of control over environment etc).  I have very limited experience working with threads.  Is there a way to use threads to achieve what i am trying to do?

    Thanks

    James

    Friday, January 2, 2009 10:26 AM
  • User2104673743 posted

    The scriptTimeOut property is perfect for you, you can set it programmatically to the time you expect it to run for and then (in the finally block for example) restablish its old value.

    Check this out:

    http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.scripttimeout.aspx

    It is easier than using threads [;)]

    Friday, January 2, 2009 11:28 AM
  • User1040660288 posted

    I didn't want to complicate the question, but it is not so much full page postbacks that i am working with, but partial page postbacks via an updatepanel.  I want very fine control over the max time for an update panel refresh.

    The trouble with scriptTimeOut is that it actually works on a 15 second timeout test cycle (so timeout could be +/- 15 seconds from time specified) - see here: http://blogs.msdn.com/pedram/archive/2007/10/02/how-the-execution-timeout-is-managed-in-asp-net.aspx?CommentPosted=true#commentmessage

    I think the answer lies in calling the thread.abort on the main HTTP thread, and somehow passing enough info (via a stateInfo object) to get it to throw a TimeOut Exception instead of an Abort Excepton.  The trouble is, I am not sure how to hook these things up, or even find the main HTTP thread again from within the spawned thread.

    Any further advice appreciated.

    Friday, January 2, 2009 1:32 PM
  • User1040660288 posted

    Another problem with the standard RequestTimeout is that it basically aborts the thread.  As far as i can tell, there is no opportunity to catch the exception and handle any differently than redirecting the user to an error page.  In my case I would simply like to display something different in the update panel.

     Any ideas anyone?

    Friday, January 2, 2009 1:53 PM
  • User1616291716 posted

    Hi jshallard,

    It seems that you want to send a friendly message to the user when the request is TimeOut, If yes, I suggest you try to achieve it by refering to the following code:

     

    Imports System 
    Imports System.Collections.Generic 
    Imports System.Linq 
    Imports System.Text 
    Imports System.Web 
    
    Public Class MyHttpModule 
        Implements IHttpModule 
        Public Sub Init(ByVal ap As HttpApplication) 
            AddHandler ap.PreSendRequestHeaders, AddressOf ap_PreSendRequestHeaders 
        End Sub 
        
        
        Private Sub ap_PreSendRequestHeaders(ByVal sender As Object, ByVal e As EventArgs) 
            Dim hr As HttpResponse = DirectCast(sender, HttpApplication).Context.Response 
            If hr.StatusCode = 408 Then ' The request time out
                hr.Redirect("TimeOut.htm") 
            End If 
        End Sub 
        Public Sub Dispose() 
        End Sub 
    End Class 
    
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, January 4, 2009 9:23 PM