WebBrowser Control In Loop

C

Chuck

I have a bunch of webpages I need to print out.
So I tried doing:

PDataContext dc = new PDataContext();
var q = from a in dc.Reviews.Take(2) select a;

System.Windows.Forms.WebBrowser browser = new
System.Windows.Forms.WebBrowser();

foreach (Review review in q)
{
browser.Navigate( @"http://x.gov/P.aspx?ReviewID=" +
review.ID);
while (browser.ReadyState != WebBrowserReadyState.Complete)
System.Threading.Thread.Sleep(100);
browser.Print();
}

In this scenario the ReadyState never goes to Complete.

I tried adding an eventhandler: Browser_DocumentCompleted
instead of using ReadyState and Sleep and putting the print method in it
but it only fired once.

I'm guessing their is some kind of thread issue where the browser doesn't
try to load until the current method is exited?

Any suggestions,

thanks,
 
Z

Zhi-Qiang Ni[MSFT]

Hi Chuck,

I assume that you used foreach loop as you did above when you tried to use
the Browser_DocumentCompleted event handler. However, this could cause an
error that only the last page will be printed, which is just as you said
"but it only fired once".

This is because within the foreach loop, the WebBrowser goes to all the
pages in the collection one after another in a very tiny time and all these
pages, except for the last one, will never be loaded completed. As the
result, the Browser_DocumentCompleted event handler will also never be
triggered.

So I suggest you having a try on this solution that makes the WebBrowser
navigate to the next page only when the last page is loaded completed.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

List<Uri> lu = new List<Uri>();
int index;

private void Form1_Load(object sender, EventArgs e)
{
lu.Add(new Uri(@"http://www.bing.com"));
lu.Add(new Uri(@"http://www.google.com"));
lu.Add(new Uri(@"http://www.asp.net"));

index = 0;

WebBrowser browser = new WebBrowser();
browser.DocumentCompleted +=new
WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted);
browser.Navigate(lu[index]);
}

private void browser_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = (WebBrowser)sender;

browser.Print();
if (index++ < lu.Count)
{
browser.Navigate(lu[index]);
}
else
{
browser.Dispose();
}
}
}

P.S. As I didn't know whether your application is based on winform or web,
I made this demo in a windows application. If you can confirm the issue
environment, it will be much more helpful for us to give a better solution.
 
Z

Zhi-Qiang Ni[MSFT]

Hi Chuck,

This is Zhi-Qiang Ni from MSDN Managed Newsgroup support team, since I
haven't seen your reply after I last posted my reply, I'm writing to check
the status of this post. Please feel free to let me know if there's
anything else I can help.
--
I assume that you used foreach loop as you did above when you tried to use
the Browser_DocumentCompleted event handler. However, this could cause an
error that only the last page will be printed, which is just as you said
"but it only fired once".

This is because within the foreach loop, the WebBrowser goes to all the
pages in the collection one after another in a very tiny time and all these
pages, except for the last one, will never be loaded completed. As the
result, the Browser_DocumentCompleted event handler will also never be
triggered.

So I suggest you having a try on this solution that makes the WebBrowser
navigate to the next page only when the last page is loaded completed.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

List<Uri> lu = new List<Uri>();
int index;

private void Form1_Load(object sender, EventArgs e)
{
lu.Add(new Uri(@"http://www.bing.com"));
lu.Add(new Uri(@"http://www.google.com"));
lu.Add(new Uri(@"http://www.asp.net"));

index = 0;

WebBrowser browser = new WebBrowser();
browser.DocumentCompleted +=new
WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted);
browser.Navigate(lu[index]);
}

private void browser_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = (WebBrowser)sender;

browser.Print();
if (index++ < lu.Count)
{
browser.Navigate(lu[index]);
}
else
{
browser.Dispose();
}
}
}

P.S. As I didn't know whether your application is based on winform or web,
I made this demo in a windows application. If you can confirm the issue
environment, it will be much more helpful for us to give a better solution.

--
Sincerely,
Zhi-Qiang Ni
Microsoft Online Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

MSDN Managed Newsgroup support offering is for non-urgent issues where an
initial response from the community or a Microsoft Support Engineer within
2 business day is acceptable. Please note that each follow up response may
take approximately 2 business days as the support professional working with
you may need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations that require urgent,
real-time or phone-based interactions. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
 
S

SC Vinod

Hi Zhi-Qiang Ni,

Thanks for your solution. I almost 100 urls in my List and I need to print them all. I have implemented your coding and i'm getting this "res://ieframe.dll/preview.js" javascript error. Can you tell me what could cause this error?? Thank you.


I have a bunch of webpages I need to print out.
So I tried doing:

PDataContext dc = new PDataContext();
var q = from a in dc.Reviews.Take(2) select a;

System.Windows.Forms.WebBrowser browser = new
System.Windows.Forms.WebBrowser();

foreach (Review review in q)
{
browser.Navigate( @"http://x.gov/P.aspx?ReviewID=" +
review.ID);
while (browser.ReadyState != WebBrowserReadyState.Complete)
System.Threading.Thread.Sleep(100);
browser.Print();
}

In this scenario the ReadyState never goes to Complete.

I tried adding an eventhandler: Browser_DocumentCompleted
instead of using ReadyState and Sleep and putting the print method in it
but it only fired once.

I am guessing their is some kind of thread issue where the browser does not
try to load until the current method is exited?

Any suggestions,

thanks,
On Tuesday, April 20, 2010 8:49 AM v-zhiqn wrote:
Hi Chuck,

I assume that you used foreach loop as you did above when you tried to use
the Browser_DocumentCompleted event handler. However, this could cause an
error that only the last page will be printed, which is just as you said
"but it only fired once".

This is because within the foreach loop, the WebBrowser goes to all the
pages in the collection one after another in a very tiny time and all these
pages, except for the last one, will never be loaded completed. As the
result, the Browser_DocumentCompleted event handler will also never be
triggered.

So I suggest you having a try on this solution that makes the WebBrowser
navigate to the next page only when the last page is loaded completed.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

List<Uri> lu = new List<Uri>();
int index;

private void Form1_Load(object sender, EventArgs e)
{
lu.Add(new Uri(@"http://www.bing.com"));
lu.Add(new Uri(@"http://www.google.com"));
lu.Add(new Uri(@"http://www.asp.net"));

index = 0;

WebBrowser browser = new WebBrowser();
browser.DocumentCompleted +=new
WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted);
browser.Navigate(lu[index]);
}

private void browser_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = (WebBrowser)sender;

browser.Print();
if (index++ < lu.Count)
{
browser.Navigate(lu[index]);
}
else
{
browser.Dispose();
}
}
}

P.S. As I did not know whether your application is based on winform or web,
I made this demo in a windows application. If you can confirm the issue
environment, it will be much more helpful for us to give a better solution.
On Friday, April 23, 2010 9:01 AM v-zhiqn wrote:
Hi Chuck,

This is Zhi-Qiang Ni from MSDN Managed Newsgroup support team, since I
have not seen your reply after I last posted my reply, I am writing to check
the status of this post. Please feel free to let me know if there is
anything else I can help.
--
I assume that you used foreach loop as you did above when you tried to use
the Browser_DocumentCompleted event handler. However, this could cause an
error that only the last page will be printed, which is just as you said
"but it only fired once".

This is because within the foreach loop, the WebBrowser goes to all the
pages in the collection one after another in a very tiny time and all these
pages, except for the last one, will never be loaded completed. As the
result, the Browser_DocumentCompleted event handler will also never be
triggered.

So I suggest you having a try on this solution that makes the WebBrowser
navigate to the next page only when the last page is loaded completed.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

List<Uri> lu = new List<Uri>();
int index;

private void Form1_Load(object sender, EventArgs e)
{
lu.Add(new Uri(@"http://www.bing.com"));
lu.Add(new Uri(@"http://www.google.com"));
lu.Add(new Uri(@"http://www.asp.net"));

index = 0;

WebBrowser browser = new WebBrowser();
browser.DocumentCompleted +=new
WebBrowserDocumentCompletedEventHandler(browser_DocumentCompleted);
browser.Navigate(lu[index]);
}

private void browser_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = (WebBrowser)sender;

browser.Print();
if (index++ < lu.Count)
{
browser.Navigate(lu[index]);
}
else
{
browser.Dispose();
}
}
}

P.S. As I did not know whether your application is based on winform or web,
I made this demo in a windows application. If you can confirm the issue
environment, it will be much more helpful for us to give a better solution.

--
Sincerely,
Zhi-Qiang Ni
Microsoft Online Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

MSDN Managed Newsgroup support offering is for non-urgent issues where an
initial response from the community or a Microsoft Support Engineer within
2 business day is acceptable. Please note that each follow up response may
take approximately 2 business days as the support professional working with
you may need further investigation to reach the most efficient resolution.
The offering is not appropriate for situations that require urgent,
real-time or phone-based interactions. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top