【问题标题】:c# WebBrowser Print() Thread in ConsoleAppc# WebBrowser Print() 线程在 ConsoleApp
【发布时间】:2017-07-25 18:55:46
【问题描述】:

我正在探索在控制台应用程序中使用 WebBrowser.Print 方法打印网页的选项。 我确实希望 PrintDialog 出现。我使用了来自WebBrowser Control in a new thread 的代码 它确实给了我一个很好的起点。

我的代码看起来:

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
            }
        }

    }
}

我没有在 browser_DocumentCompleted 方法中弹出“Application.ExitThread();”。当我将其注释掉时,我会弹出但线程不存在并且我的控制台应用程序继续运行。如何退出我的应用程序?任何指针?谢谢大家!

【问题讨论】:

标签: c# multithreading webbrowser-control


【解决方案1】:

我使用了这篇文章并对其进行了修改以满足我的要求。 Print WebBrowser without previewing i.e. single click print 感谢https://stackoverflow.com/users/1768303/noseratio

【讨论】:

    猜你喜欢
    • 2011-12-13
    • 1970-01-01
    • 2022-01-17
    • 2010-11-18
    • 1970-01-01
    • 2014-06-10
    • 2021-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多