WebBrowser controllo - Application Console - Eventi Non Firing
-
22-08-2019 - |
Domanda
Sono stato navigavo domande i vari controllo WebBrowser StackOverflow , e io non riesco a trovare una risposta ad un problema che sto avendo. Sto cercando di utilizzare il controllo WebBrowser href="https://stackoverflow.com/questions/610183/printing-webbrowser-control-content"> . A seguito di di MSDN esempio , ho creato la seguente applicazione di console:
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();
}
}
}
Tuttavia, l'evento DocumentCompleted non si attiva mai. E 'possibile utilizzare questo controllo Windows.Forms in un'applicazione console?
Soluzione
Il requisito fondamentale di un thread STA è che ha bisogno di eseguire un message pump. In Windows Form, è possibile utilizzare Application.Run. Oppure si potrebbe scrivere la pompa messaggio a mano, utilizzando user32! GetMessage & DispatchMessage. Ma è probabilmente più facile da usare quella in WinForms o WPF.
Altri suggerimenti
Si lavorerà più a lungo eventi di processo mentre è in esecuzione.
Si può semplicemente chiamare "Application.DoEvents ()" in un ciclo di attesa. Non è necessario fare qualcosa di più elaborato di quello, e funziona benissimo nella mia applicazione di console.