PC Review


Reply
Thread Tools Rate Thread

Help with event response in threadpool.

 
 
Archer
Guest
Posts: n/a
 
      24th Mar 2006
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?

 
Reply With Quote
 
 
 
 
Jason Hales
Guest
Posts: n/a
 
      24th Mar 2006
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;
}
}
}

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Event ID: 1003, Event Type: Error ,Event Source: System Error,Event Category: (102) BoazBoaz Windows XP General 0 21st Jun 2006 05:39 PM
Response to Last Response in "Help with HP 5650" Post Raymond A. Chamberlin Printers 0 19th May 2004 05:12 AM
Response Library Add-In - Response Templates! Lookyt Microsoft Outlook 4 7th Apr 2004 03:28 AM
Help! Help! Help! Help! Help! Help! Help! Help! Help! Help! Help! Help! Help! -$- Windows XP Internet Explorer 2 21st Dec 2003 11:45 PM
Auto Response or Vacation Response M. Abell Microsoft Outlook 1 2nd Dec 2003 04:51 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:19 PM.