We help IT Professionals succeed at work.

Clearing Cache System.Windows.Forms.WebBrowser

Envision11
Envision11 asked
on
I am currently creating a System.Windows.Forms.WebBrowser() to render a page then convert that page to a Bitmap to used in a PDF.
This all works with out issue, but the WebBrowser() keeps caching the same stylesheet which can be selected by the user.
So i am looking for a way to completely clear the cache of this browser each time its called.( Clearing FF / IE has not affect )

			private void CreateHtml2BitmapThread(object _Params)
			{
				System.Windows.Forms.WebBrowser Browser = null;

				ConvertHtmlToBitmapParams Params = (_Params as ConvertHtmlToBitmapParams);

				try
				{
					Browser = new System.Windows.Forms.WebBrowser();
					Browser.Refresh(System.Windows.Forms.WebBrowserRefreshOption.Completely);
					Browser.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(IsDocumentCompleted);
					Browser.Size = new Size(DOCUMENT_WIDTH,DOCUMENT_HEIGHT);
					Browser.ScrollBarsEnabled = false;
					
					Browser.Navigate(new Uri(Params.m_sURL));

					int Timeout = 100;
					while ((!bIsDocumentCompleted) || (Timeout <= 0))
					{
						System.Windows.Forms.Application.DoEvents();
						Thread.Sleep(100);
						Timeout--;
					}

					if (bIsDocumentCompleted)
					{
						Bitmap TempBitmap = new Bitmap(BITMAP_WIDTH,BITMAP_HEIGHT);
						Browser.DrawToBitmap(TempBitmap,new Rectangle(0,0,BITMAP_WIDTH,BITMAP_HEIGHT));
						Params.m_BitmapList.Add(TempBitmap);
					}
				}
				catch (Exception _Ex)
				{
					DebugUtils.LogException(_Ex);
				}

				if (Browser != null)
				{
					Browser.Dispose();
				}

				Params.m_bHaveGotData = true;
			}

			private void IsDocumentCompleted(object sender,System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
			{
				bIsDocumentCompleted = true;
			}

Open in new window

Comment
Watch Question

Commented:
You will have to delete the cache-entries yourself,
See http://support.microsoft.com/kb/326201 for a complete c#-source.
 
Try this....
<head>
  <meta http-equiv="Expires" CONTENT="0">
  <meta http-equiv="Cache-Control" CONTENT="no-cache">
  <meta http-equiv="Pragma" CONTENT="no-cache">
</head>

or in asp

Response.CacheControl = "no-cache"
Response.AddHeader "Pragma", "no-cache"
Response.AddHeader "cache-control","private"
Response.Expires = -1
Response.Expiresabsolute = Now() - 1

Server-side:

private void Page_Load(object sender, System.EventArgs e)
{
 if ( !this.IsPostBack )
 {
  // This is the initial load of the page...
 }
}

Client-side:

<script type=text/javascript>
<!--
function windowOnLoad()
{
 var serverSideIsPostBack = '<%= IsPostBack %>';
 var isPostBack = (serverSideIsPostBack == 'True') ? true : false;

 if ( isPostBack == false )
 {
  // This is the initial load of the page...
 }
}
window.onload = windowOnLoad;
// -->
</script>

Hope this help.