Solved

WebBrowser control, questions about NavigateError event

Posted on 2004-04-07
5
2,095 Views
Last Modified: 2013-12-26
I've got a web page that is actually a time and attendance timeclock.  This web page refreshes once a minute to provide an updated time on the page, equal to the host's time.  Occasionally the host is not available and the page ends in a HTTP 500 (Internal Server Error) code.

Some of my end users are not sofisticated and don't really understand why the HTTP 500 screen appears instead of the clock display.  To them, it's simply "down".  If I do a refresh (F5), 99.9% of the time, the clock refreshes and I'm back in business.  I can't expect the end users to do this because they don't know that trick and it's far too many people to try to train.  The clock should just work, period.

So my solution was to write a VB program that displayed my clock within the WebBrowser control.  It has some neat events such as BeforeNavigate, NavigateComplete, and NavigateError.  I thought I was home free when I saw that I could get a NavigateError event raised.  In that event, I would simply fire off another refresh.

Now here's the problem:

This is difficult to explain but if I start off with a good call, I get my screen, everything is wonderful.  If I then shut down my HTTP server, I get the HTTP 500 screen.  Also good.  My app sees the navigation error and starts a timer which fires off a refresh (basically a new Navigate method).  This process continues until I restatr my server.  At that time my screen displays with thee updated time and everything is fine.  Now to test this thing thoroughly, I stop my HTTP server again.  This time however, I still get the NavigateError event, which then causes a new Navigate by design, but instead of getting the HTTP 500 screen, I get a previous, what appears to be a cached version of, my clock screen.  I can tell this because the time displayed will be the time of the first HTTP shutdown and not the second.  In fact now, if I continue to start and stop my HTTP server, I'll always get the last time I stopped the server.  I never get the HTTP 500 screen anymore.

One approach I've tried was to set a flag, bNavErr in the BeforeNavigate event to False.  Then if NavigateError fires, I set the flag to True.  On my NavigateComplete event, I check this flag and if it's True, I can assume I had a navigation error, so I turn on my timer and start refreshing.  In this case I flip-flop between a cached screen and an HTTP error screen for each time the Navigate method is called.

I don't see a means of clearing the cache but I'm wonder if that could be done.  Another approach may be to close, then reopen a WebBrowser object but I'm trying to avoid that for performance reasons.  




0
Comment
Question by:mskvarenina
5 Comments
 
LVL 11

Accepted Solution

by:
bingie earned 250 total points
ID: 10777601
Try the delete cache API.

'Place the following in the top of your coded module, after option explicit:
Private Declare Function DeleteUrlCacheEntry Lib "wininet.dll" Alias "DeleteUrlCacheEntryA" (ByVal lpszUrlName As String) As Long

'Place the following in the Document_Complete event of the webbrowser
DeleteUrlCacheEntry (webbrowser.LocationURL)

Then this will always return the "freshest" page.

Also, when using the .Refresh method, use the ,1 after it.

i.e. webbrowser1.Refresh, 1 which will force a complete refresh from server not cache.

Bingie
0
 

Author Comment

by:mskvarenina
ID: 10777824
Bingie,  excellent answer!  Your suggestion worked just fine.  I figured there be an API to clear the cache, I just didn't know where to look.

1 question for you:

1, Is there a difference between doing another .Navigate2 vs. a .Refresh?  My code today just does a new .Navigate2 as the refresh.

0
 
LVL 11

Expert Comment

by:bingie
ID: 10777976
AFAIK, The .Navigate2 will not do the refresh from cache like the .Refresh will.

I would use the .Navigate2 only if you are going to a new URL, which will download a fresh version providing the page isn't cached.
0
 

Expert Comment

by:solaire
ID: 10844601
I'm trying to read several different pages.  If I interrupt my code within the loop then I get the page that I want, otherwise it seems to only read the last page.  Should I put some code in to represent my interruption?  And if, so what?

for i=1 to 5
 
  WebBrowser1.Navigate2 Address(i)
  WebBrowser2.refresh2 1
  Do while WebBrowser1.Busy
    DoEvents
  Loop
  s=WebBrowser1.Document.body.innertext
  MySearchString(i)=mid$(s,3000,50)

Next i
0
 

Expert Comment

by:howieba
ID: 11074882
solaire, two comments for you:

1. I had this problem with the WebBrowser control and fixed just yesterday. The issue is the DoEvents - it blocks not allowing the WebBrowser messages to get CPU cycles to complete as you see when it immediately completes as you break. You need to use a DoEvents replacement which you can pick up at:

http://members.lycos.co.uk/nirsoft1/vb/doevents.html

2. If you are looking to simply have your program retrieve some web pages without the need for an interactive web control on your form(s), you can use the MSHTML Object Library instead.

a) Take the web control off your form.
b) include a reference for MSHTML Object Library
c) your code snippet above becomes:

Dim objLink As HTMLLinkElement
Dim objMSHTML As New MSHTML.HTMLDocument
Dim objDocument As MSHTML.HTMLDocument


for i=1 to 5

  Set objDocument = objMSHTML.createDocumentFromUrl(Address(i), vbNullString)

  While objDocument.readyState <> "complete"
      MyDoEvents                                ' you still need the DoEvents replacement
  Wend

  s=objDocument.body.innertext
  MySearchString(i)=mid$(s,3000,50)

  Set objDocument = Nothing
next i
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to inactivate a tab on a form based on permissions? 11 63
Vb.net Listview 9 76
Is an IP address in a Range 22 71
VBA to format 3 79
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now