Reputation: 2620
I am exploring the option of Printing a webpage using WebBrowser.Print method in a console application. I do want PrintDialog to show up. I have used code from WebBrowser Control in a new thread It does give me a good starting point.
My Code looks :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;
namespace STAThreadConsoleApp
{
class Program
{
static void Main(string[] args)
{
Uri url = new Uri("http://www.google.com");
runBrowserThread(url);
}
static void runBrowserThread(Uri url)
{
var th = new Thread(() => {
var br = new WebBrowser();
br.DocumentCompleted += browser_DocumentCompleted;
br.Navigate(url);
Application.Run();
});
th.SetApartmentState(ApartmentState.STA);
th.Start();
}
static void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
var br = sender as WebBrowser;
if (br.Url == e.Url)
{
Console.WriteLine("Natigated to {0}", e.Url);
br.ShowPrintDialog();
Application.ExitThread(); // Stops the thread
}
}
}
}
I dont get pop up with "Application.ExitThread();
" in the browser_DocumentCompleted method. When I comment it out, I do get pop up but thread doesn't exist and my console app continues to run. How to get to exit my application? Any pointers? Thanks all !
Upvotes: 2
Views: 449
Reputation: 2620
I used this post and modified it to suit my requirements. Print WebBrowser without previewing i.e. single click print Thanks to https://stackoverflow.com/users/1768303/noseratio
Upvotes: 1