Printing WebBrowser控制-控制台应用程序-事件未触发

Printing WebBrowser控制-控制台应用程序-事件未触发,printing,browser,webbrowser-control,console-application,Printing,Browser,Webbrowser Control,Console Application,我一直在浏览各种各样的网站,但我似乎找不到解决我遇到的问题的答案。我正试着用这个。下面,我创建了以下控制台应用程序: namespace WebPrintingMadness { using System; using System.Collections.Generic; using System.Text; /// <summary> /// The entry point of the program. /// </sum

我一直在浏览各种各样的网站,但我似乎找不到解决我遇到的问题的答案。我正试着用这个。下面,我创建了以下控制台应用程序:

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 = "https://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();
        }
    }
}
名称空间WebPrintingMadness
{
使用制度;
使用System.Collections.Generic;
使用系统文本;
/// 
///程序的入口点。
/// 
班级计划
{
/// 
///程序的主要入口点。
/// 
///程序参数。
[状态线程]
公共静态void Main(字符串[]args)
{
字符串url=”https://stackoverflow.com/";
WebPagePrinter WebPagePrinter=新的WebPagePrinter();
网页打印机。打印网页(url);
Console.ReadLine();
}
}
}
名称空间WebPrintingMadness
{
使用制度;
使用System.Collections.Generic;
使用系统文本;
使用System.Windows.Forms;
/// 
///此类用于打印网页。
/// 
内部类网页打印机:IDisposable
{
/// 
///System.Windows.Forms.WebBrowser控件。
/// 
私人网络浏览器;
/// 
///初始化WebPagePrinter类的新实例。
/// 
内部网页打印机()
{
this.webBrowser=新的webBrowser();
this.webBrowser.DocumentCompleted+=新的WebBrowserDocumentCompletedEventHandler(this.webBrowser\u DocumentCompleted);
this.webBrowser.ScriptErrorsSuppressed=true;
}
/// 
///处理这个实例。
/// 
公共空间处置()
{
这个。处置(真实);
总干事(本);
}
/// 
///打印网页。
/// 
///网页的url。
内部无效打印网页(字符串url)
{   
this.webBrowser.Navigate(url);
}
/// 
///处理这个实例。
/// 
///如果为True,则为True,否则为false。
受保护的虚拟void Dispose(bool disposing)
{
如果(处置)
{
如果(this.webBrowser!=null)
{
this.webBrowser.Dispose();
this.webBrowser=null;
}
}
}
/// 
///webBrowser DocumentCompleted事件的事件处理程序。
/// 
///事件发送者。
///事件参数。
私有void WebBrowser_DocumentCompleted(对象发送方,WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser navigated=发送方为WebBrowser;
如果(导航==null)
{
返回;
}
navigated.Print();
navigated.Dispose();
}
}
}

但是,DocumentCompleted事件从不激发。是否可以在控制台应用程序中使用此Windows.Forms控件?

STA线程的基本要求是需要运行消息泵。
在Windows窗体中,可以使用Application.Run。或者,您可以使用user32手工编写消息pump!GetMessage和DispatchMessage。但是在WinForms或WPF中使用它可能更容易。

只要在运行时处理事件,它就可以工作


您只需在等待循环中调用“Application.DoEvents()”。您不需要做任何比这更花哨的事情,它在我的控制台应用程序中运行良好。

看起来jachymko是对的,所以我将把它转换成一个“静默”WinForms应用程序。您能回答我的问题吗>?