views:

2838

answers:

2

I have been navigating the various WebBrowser control stackoverflow questions, and I can't seem to find an answer to a problem I am having. I am trying to use the WebBrowser control to print a web page. Following MSDN's example, I have created the following console application:

namespace WebPrintingMadness
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    /// <summary>
    /// The entry point of the program.
    /// </summary>
    class Program
    {
        /// <summary>
        /// The main entry point of the program.
        /// </summary>
        /// <param name="args">Program arguments.</param>
        [STAThread]
        public static void Main(string[] args)
        {
            string url = "http://stackoverflow.com/";

            WebPagePrinter webPagePrinter = new WebPagePrinter();
            webPagePrinter.PrintWebPage(url);
            Console.ReadLine();
        }
    }
}



namespace WebPrintingMadness
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;

    /// <summary>
    /// This class is used to print a web page.
    /// </summary>
    internal class WebPagePrinter : IDisposable 
    {
        /// <summary>
        /// A System.Windows.Forms.WebBrowser control.
        /// </summary>
        private WebBrowser webBrowser;

        /// <summary>
        /// Initializes a new instance of the WebPagePrinter class.
        /// </summary>
        internal WebPagePrinter()
        {
            this.webBrowser = new WebBrowser();
            this.webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(this.WebBrowser_DocumentCompleted);
            this.webBrowser.ScriptErrorsSuppressed = true;
        }

        /// <summary>
        /// Disposes of this instance.
        /// </summary>
        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Prints a web page.
        /// </summary>
        /// <param name="url">The url of the web page.</param>
        internal void PrintWebPage(string url)
        {   
            this.webBrowser.Navigate(url);
        }

        /// <summary>
        /// Disposes of this instance.
        /// </summary>
        /// <param name="disposing">True if disposing, otherwise false.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (this.webBrowser != null)
                {
                    this.webBrowser.Dispose();
                    this.webBrowser = null;
                }
            }
        }

        /// <summary>
        /// Event handler for the webBrowser DocumentCompleted event.
        /// </summary>
        /// <param name="sender">The event sender.</param>
        /// <param name="e">The event arguments.</param>
        private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser navigated = sender as WebBrowser;

            if (navigated == null)
            {
                return;
            }

            navigated.Print();
            navigated.Dispose();
        }
    }
}

However, the DocumentCompleted event never fires. Is it possible to use this Windows.Forms control in an console application?

+1  A: 

The basic requirement of an STA thread is that it needs to run a message pump. In Windows Forms, you can use Application.Run. Or you could write the message pump by hand, using user32!GetMessage & DispatchMessage. But it's probably easier to use the one in WinForms or WPF.

jachymko
Would it be possible for you to answer my question <a href="http://stackoverflow.com/questions/764869/c-console-app-event-handling">here</a>?
Sean Ochoa
A: 

It will work as long as process events while it's running.

You can simply call "Application.DoEvents()" in a wait loop. You don't need to do anything fancier than that, and it works fine in my console application.

Mikey