WebBrowser Control In Loop

Discussion in 'ASP .Net Web Controls' started by Chuck, Apr 16, 2010.

  1. Chuck

    Chuck Guest

    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,
    Chuck, Apr 16, 2010
    #1
    1. Advertising

  2. 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.

    --
    Sincerely,
    Zhi-Qiang Ni
    Microsoft Online Support
    Zhi-Qiang Ni[MSFT], Apr 20, 2010
    #2
    1. Advertising

  3. 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
    ==================================================
    Zhi-Qiang Ni[MSFT], Apr 23, 2010
    #3
  4. Chuck

    SC Vinod Guest

    Javascript error while printing

    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.



    > On Friday, April 16, 2010 4:38 PM Chuck wrote:


    > 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.
    >>
    >> --
    >> Sincerely,
    >> Zhi-Qiang Ni
    >> Microsoft Online Support



    >>> 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
    >>> ==================================================



    >>> Submitted via EggHeadCafe - Software Developer Portal of Choice
    >>> Composite UI Pattern and RAD Development for Data Entry Applications, Part 1
    >>> http://www.eggheadcafe.com/tutorial...pment-for-data-entry-applications-part-1.aspx
    SC Vinod, Sep 7, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel Frede

    ASPNET and Webbrowser control

    Daniel Frede, May 17, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    4,568
    Ken Cox [Microsoft MVP]
    May 18, 2004
  2. ASP .NET Newbie
    Replies:
    10
    Views:
    14,723
    jojobouy
    Sep 28, 2007
  3. clintonG
    Replies:
    3
    Views:
    586
    Hermit Dave
    Sep 25, 2004
  4. JP2006
    Replies:
    1
    Views:
    1,063
    Rick Strahl [MVP]
    Jan 7, 2007
  5. Isaac Won
    Replies:
    9
    Views:
    370
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page