Help with event response in threadpool.

  • Thread starter Thread starter Archer
  • Start date Start date
A

Archer

I am using a controls, webbrowser, in threadpool. every thread
has its own webbrowser object. at the same time, i must response for
the event DocumentComplete, i don't know whether the event should be
fired when the thread method returned?

How to realize this?
 
Are you asking whether you should call Navigate() and then exit or wait
until documentComplete has fired and then exit?

I've written a web crawler in C# that calls Navigate and then goes into
a loop (yielding via Application.DoEvents) waiting until
DocumentComplete is fired. At that stage you can call your call back
delegate.

Here's the sample code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using mshtml;

namespace WebBrowserNavigator
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class NavigatorForm : System.Windows.Forms.Form
{
private AxSHDocVw.AxWebBrowser axWebBrowser;
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Label lblStatus;
private bool documentComplete;

public NavigatorForm()
{
InitializeComponent();
}


public string NavigateTo(string url)
{
SetStatus("NagigateTo " + url);
string html = null;

documentComplete = false;
Goto(url);

const int MAX_WAIT = 50000;
int counter = 0;
while (! documentComplete)
{
counter++;
SetStatus("Waiting " + counter);
Application.DoEvents();

if (counter > MAX_WAIT)
{
break;
}
}

SetStatus("Checking title");
IHTMLDocument2 htmlDocument = (IHTMLDocument2)axWebBrowser.Document;
if (htmlDocument.title != "Cannot find server" && htmlDocument.title !=
"No page to display")
{
html = htmlDocument.body.innerHTML;
}

Application.DoEvents();

return html;
}

private void SetStatus(string message)
{
lblStatus.Text = message;
Application.DoEvents();
}

private void Goto(string url)
{
object o = null;
axWebBrowser.Navigate(url, ref o, ref o, ref o, ref o);
SetStatus("Goto " + url);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(NavigatorForm));
this.axWebBrowser = new AxSHDocVw.AxWebBrowser();
this.lblStatus = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.axWebBrowser)).BeginInit();

this.SuspendLayout();
//
// axWebBrowser
//
this.axWebBrowser.Enabled = true;
this.axWebBrowser.Location = new System.Drawing.Point(4, 4);
this.axWebBrowser.OcxState =
((System.Windows.Forms.AxHost.State)(resources.GetObject("axWebBrowser.OcxState")));

this.axWebBrowser.Size = new System.Drawing.Size(424, 264);
this.axWebBrowser.TabIndex = 0;
this.axWebBrowser.DocumentComplete += new
AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(this.axWebBrowser_DocumentComplete);

//
// lblStatus
//
this.lblStatus.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblStatus.Dock = System.Windows.Forms.DockStyle.Bottom;
this.lblStatus.Location = new System.Drawing.Point(4, 37);
this.lblStatus.Name = "lblStatus";
this.lblStatus.Size = new System.Drawing.Size(248, 16);
this.lblStatus.TabIndex = 1;
this.lblStatus.Text = "Ready";
//
// NavigatorForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(256, 57);
this.Controls.Add(this.lblStatus);
this.Controls.Add(this.axWebBrowser);
this.DockPadding.All = 4;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));

this.Name = "NavigatorForm";
((System.ComponentModel.ISupportInitialize)(this.axWebBrowser)).EndInit();

this.ResumeLayout(false);

}
#endregion


private void axWebBrowser_DocumentComplete(object sender,
AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)
{
SetStatus("Document complete");
documentComplete = true;
}
}
}
 
Back
Top