Force a subroutine to close

Hi Experts,

The code below uses a timer to start a subroutine every ten minutes to scrape some information from a web page. Sometimes the web page being scraped does not load and the subroutine hangs. When the timer ticks again the subroutine is started again and an attempt to scrape the page is started again.

If the subroutine hangs how do I  force it to terminate so when the timer ticks so a new instance of the subroutine is started?

    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick

        DoPageScrape()

    End Sub

Open in new window

DColinAsked:
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.

Jacques Bourgeois (James Burger)PresidentCommented:
Understand what happens in the subroutine when it hangs, and find a workaround.

It might go into an infinite loop, trigger and Exception or something else. Most situations can be controlled once you understand where the problem is.

For instance, an infinite loop can be forced to exit after a certain number of iterations or certain delay.

An Exception can be trapped in a Try..Catch... End Try block and handled according to the Exception that is triggered.

The solution depends on the code that hangs.
0
DColinAuthor Commented:
JamesBurger,

I know what is causing the subroutine to hang it is that a page stops loading in a webbrowser control. How would I detect that a page has not loaded after ten minutes?
0
Bob LearnedCommented:
You should look into asynchronous calls, with cancellation, as shown here:

Cancellation in Managed Threads
http://msdn.microsoft.com/en-us/library/dd997364.aspx

Shared Sub CancelWithThreadPoolMiniSnippet()


    'Thread 1: The Requestor 
    ' Create the token source. 
    Dim cts As New CancellationTokenSource()

    ' Pass the token to the cancelable operation.
    ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf DoSomeWork), cts.Token)

    ' Request cancellation by setting a flag on the token.
    cts.Cancel()
    ' end block 
End Sub 

'Thread 2: The Listener 
Shared Sub DoSomeWork(ByVal obj As Object)

    Dim token As CancellationToken = CType(obj, CancellationToken)
    For i As Integer = 0 To 1000000

        ' Simulating work.
        Thread.SpinWait(5000000)

        If token.IsCancellationRequested Then 

            ' Perform cleanup if necessary. 
            '... 
            ' Terminate the operation. 
            Exit For 
        End If 
    Next 
End Sub

Open in new window

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
Jacques Bourgeois (James Burger)PresidentCommented:
Can you show us the code of the method and point to the line where it hangs?
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
Visual Basic.NET

From novice to tech pro — start learning today.