0

New to the threading and its concepts. I have following code where I am using Windows.Forms in console app to print a webpage.

My Code looks like below:

My Application does print the page but it never exists . its stuck at Application.Run();

How do I make my application exit? Thanks for helping out.

( if I use Application.DoEvents(); wb.print(); does not print. )

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WebBrowserWithoutAForm
{
    class Program
    {
        private static bool completed = false;
        private static WebBrowser wb;
        [STAThread]
        static void Main(string[] args)
        {
            wb = new WebBrowser();
            wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentCompleted);
            wb.Navigate("http://www.google.com");

            while (!completed)
            {
                //Application.DoEvents();
                Application.Run();
            }
            Console.Write("\n\nDone with it!\n\n");
            Console.ReadLine();
        }
        static void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            Console.WriteLine(wb.Document.Body.InnerHtml);
            wb.Print();
            completed = true;
        }
    }
}
ProgSky
  • 2,530
  • 8
  • 39
  • 65
  • 2
    Please don't ever use `Application.DoEvents()`. It's only in the framework for backward compatibility with VB6. It will create hard-to-resolve bugs in your code. It is always a better option to use something else. – Enigmativity Jul 21 '17 at 05:20

1 Answers1

2

MSDN for Application.Run says:

In a Win32-based or Windows Forms application, a message loop is a routine in code that processes user events, such as mouse clicks and keyboard strokes. Every running Windows-based application requires an active message loop, called the main message loop. When the main message loop is closed, the application exits. In Windows Forms, this loop is closed when the Exit method is called, or when the ExitThread method is called on the thread that is running the main message loop.

The Exit method this talks about is Application.Exit.

tymtam
  • 31,798
  • 8
  • 86
  • 126