[newbie] Webbrowser question

Hello all,

I am working on an application that will print a saved Web page without displaying it to the user. I have found a code sample in the help that would do exactly this. Here is the code:

public void PrintHelpPage ()

{

WebBrowser webBrowserForPrinting = new WebBrowser();

webBrowserForPrinting.Url = new Uri (@file://c:/out.html);

webBrowserForPrinting.DocumentCompleted +=

new WebBrowserDocumentCompletedEventHandler(PrintDocument);

}

private void PrintDocument (object sender, WebBrowserDocumentCompletedEventArgs e)

{

((WebBrowser)sender).Print();

((WebBrowser)sender).Dispose();

}

Now, my problem is how to call PrintHelpPage from Main. If I call it by simply typing PrintHelpPage();, an ActieX exeption is throws saying that it is not in a single threaded apartement.

I have tryed to call it like this:

Thread t = new Thread (PrintHelpPage);

t.SetApartementState(ApartementState.STA);

t.Start();

This case the code will run, but it never enters PrintDocument. Can anyone tell me why

Any help would be appreciated.

Petya



Answer this question

[newbie] Webbrowser question

  • Jumanji_24

    Is there a reason you are not running a single threaded apartment

    If you change "[MTAThread]" attribute to "[STAThread]" on your entry point the first code you written should work.

     



  • Viking_p

    I am trying to do what this thread talks about in VisStudio 2005.
    I encounter a problem and dont know how to proceed.
    When the print method is invoked, it returns immediatly.
    Nothing gets sent to the print queue.
    I've experienced this same issue when trying to put a webbrowser object in a form too.
    The print thread must be able to run prior to the form exiting.
    Yet there is no event associated with the print method
    So how do you make the current thread stick around long enough for the print thread to complete
    Otherwise, no printing actually occurs (I've verified this in my forms based code by placing a message box following the print method call - and things get printed - remove the message box and no printing occurs.)

    Help anyone

    Thanks!

  • Franz Muheim

    I would expect the reason that your event does not get fired is because the thread has finished. You need to allow events to be fired before you finish. I made a simple test application based from your code.

    private static void PrintPage()
    {
    WebBrowser wb = new WebBrowser();
    wb.Url =
    new Uri("http://forums.microsoft.com/msdn");
    wb.DocumentCompleted +=
    new WebBrowserDocumentCompletedEventHandler(PrintDocumentCompletedHandler);

    while (wb.ReadyState != WebBrowserReadyState.Complete)
    {
    Application.DoEvents();
    Thread.Sleep(50);
    }
    }

    private static void PrintDocumentCompletedHandler(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
    if (sender is WebBrowser)
    {
    WebBrowser wb = sender as WebBrowser;
    //wb.Print();
    }
    }

    I start it off in a new thread like this.

    Thread t = new Thread(new ThreadStart(PrintPage));
    t.SetApartmentState(
    ApartmentState.STA);
    t.Start();

    Application.DoEvents() allows the event handlers to be called. If I do not perform the loop until ReadyState is Completed the events will never get an opportunity to fire.

    Alternativly I could call DoEvents() until a flag is set to true by the DocumentCompleted event handler.



  • intromicko

    Andreas,

    Thanks a lot! You have helped me a lot! I have tied it and it works!!

    So, basically my only error was that I did not let my Thread "do it's job" by saying DoEvents,and did not give it a "time-frame" to finish.

    Thanks again! You saved me a lot of help-reading time :)

    Petya


  • [newbie] Webbrowser question