?
Solved

How to rerun code after exception has been raised in ASP.NET Page Load Event?

Posted on 2010-03-26
4
Medium Priority
?
306 Views
Last Modified: 2013-11-07
Hi,

I have a Page_Load event which invokes a big task which can be memory hungry. I have been having "Out Of Memory" exceptions. So I have decided to put in a MemortFailPoint which will create "Insufficient Memory" Exceptions thus preventing the Web App from collapsing. However the memory problem is likely to be temporary since the Garbage Collector is going around all the time and releasing memory. So I want to re enter the relevant code after a period of time after the exception has been raised to try again. I guess I would try say five times and then stop. The question is what is the best way of coding this.

My Page load code may start to look the attached. I could do with some help on fleshing out the code please.

Thanks,

Sam

P.S I have profiled the memory usuage and leakage does not looks to be a big problem. However I think I might be suffering from a variable loads on a share host and variable concurrent loads on my app hence the need to put in this self protection code.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       
        Dim MFP As MemoryFailPoint
        MFP = New MemoryFailPoint(35) ' Check that 35MB available otherwise raise InsufficientMemoryException exception
        MFP.Dispose()

        Try


           'Run Big task


        Catch ex As InsufficientMemoryException

           ' Exit if 3 attempt
           ' Some sort of timer for 10 secs
           ' Re Enter Big Task
           

        End Try



    End Sub

Open in new window

0
Comment
Question by:SamJolly
  • 3
4 Comments
 
LVL 18

Accepted Solution

by:
carlnorrbom earned 2000 total points
ID: 28754088
Hi,

You could do something simple like:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       Dim MFP As MemoryFailPoint
       MFP = New MemoryFailPoint(35) ' Check that 35MB available otherwise raise InsufficientMemoryException exception
       MFP.Dispose()
       Dim i As Integer = 0
       Dim maxTries As Integer = 3 'If you want to try 3 times...
       Do While i < maxTries + 1
           Try
               'Logic to run long running task goes here
           Catch ex As InsufficientMemoryException
               If i = maxTries - 1 Then 'We've tried the maximum number of times so exit
                   Exit Sub
               End If
               System.Threading.Thread.Sleep(10000) 'Wait for 10 seconds before trying again
           End Try
       Loop
    End Sub

Not sure it's what you're after but it's a simple solution to your problem.

/Carl.
0
 

Author Comment

by:SamJolly
ID: 28966303
Carl,

Thanks for this useful code listing.

Quick question though. I have some code in "Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)" within global.asax which writes any raised exception to an error log. In all cases the user then gets redirected to a Error Feedback page. However in this case I could exclude the redirection. What I am unsure about is whether once the code flow has exitted to the global.asax then it is then lost from the page_load event. Thus I can only really use it ONCE I have tried 3 times rather than exitting 3 times, writing the error log and then finally exitting and redirecting....

I am also assumming that the global.asax gets invoked on the "End Try" rather than the "Catch" line??

Hope this makes sense,

Sam
0
 

Author Comment

by:SamJolly
ID: 28970684
Hi,

This is what I ended up with...

Cheers,

Sam

Dim MFP As MemoryFailPoint
        Dim i As Integer = 0
        Dim intTries As Integer = 0
        Dim maxTries As Integer = 3 'If you want to try 3 times...
        Do While intTries <= maxTries

            Try

                intTries = intTries + 1

                MFP = New MemoryFailPoint(1024) ' Check that 35MB available otherwise raise InsufficientMemoryException exception
                MFP.Dispose()

                'logic

            Catch ex As InsufficientMemoryException

                If intTries = maxTries Then 'We've tried the maximum number of times so exit
                    ex.Source = "Test, Insufficient Memory"
                    Throw ex
                Else
                    System.Threading.Thread.Sleep(5000) 'Wait for 5 seconds before trying again
                End If


            End Try


        Loop

Open in new window

0
 

Author Closing Comment

by:SamJolly
ID: 32334221
thx
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question