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

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

SamJollyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

carlnorrbomCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SamJollyAuthor Commented:
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
SamJollyAuthor Commented:
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
SamJollyAuthor Commented:
thx
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.