Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / March 2006

Tip: Looking for answers? Try searching our database.

Help with event response in threadpool.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Archer - 24 Mar 2006 08:52 GMT
     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?
Jason Hales - 24 Mar 2006 11:46 GMT
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;
}
}
}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.