質問

I'm trying to bulk print HTML reports to my default printer which is PDF Creator setup for auto saving. I have the HTML files loading up through Internet Explorer and from there I shall print without user prompt.

The problem I'm having is that when my program loops through to print the list of HTML files it has found that some of the documents do not spool and do not get printed. I did read on the net that this is can be solved with using a while loop and a Application.Dowork(). When I implemented this occasionally all of my documents would print which was an improvement however this was only occasionally and not a sure fix.

Could my problem be that the each thread is closing before it has finished it processing?

If so how could I get the threads to run independently so they do not close while they are still processing?

Below is the code I'm using to print the documents to the default printer:

foreach (var x in fileList)
                {

                    // Printing files through IE on default printer.
                    Console.WriteLine("{0}", x);
                    SHDocVw.InternetExplorer IE = new SHDocVw.InternetExplorer();
                    IE.DocumentComplete += new SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(IE_DocumentComplete);
                    IE.PrintTemplateTeardown += new SHDocVw.DWebBrowserEvents2_PrintTemplateTeardownEventHandler(IE_PrintTemplateTeardown);
                    IE.Visible = true;
                    IE.Navigate2(x);
                    while (IE.ReadyState != SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE)
                    {
                        System.Windows.Forms.Application.DoEvents();
                    }

                }
            }
        }
    }

    static void IE_PrintTemplateTeardown(object pDisp)
    {
        if (pDisp is SHDocVw.InternetExplorer)
        {
            SHDocVw.InternetExplorer IE = (SHDocVw.InternetExplorer)pDisp;
            IE.Quit();
            System.Environment.Exit(0);
        }
    }

    static void IE_DocumentComplete(object pDisp, ref object URL)
    {
        if (pDisp is SHDocVw.InternetExplorer)
        {
            SHDocVw.InternetExplorer IE = (SHDocVw.InternetExplorer)pDisp;
             IE.ExecWB(SHDocVw.OLECMDID.OLECMDID_PRINT, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, 2);
        }
    }
役に立ちましたか?

解決

What do you think about the following approach? I'm using the System.Windows.Forms.WebBrowser control to make the requests, inside the form's Form_Load method decorated with the async modifier. In the method, I use await to postpone the navigation to the following link until the list of printed files contains the one which is currently browsed.

namespace WindowsFormsApplication7
{
    public partial class Form1 : Form
    {
        List<string> fileList;
        List<string> printedFileList;
        public Form1()
        {
            InitializeComponent();

            fileList = new List<string>();
            printedFileList = new List<string>(); ;

            fileList.Add("http://www.google.de/");
            fileList.Add("http://www.yahoo.de/");

            webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
        }

        void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (!printedFileList.Contains(webBrowser1.Url.AbsoluteUri))
                webBrowser1.Print();
            printedFileList.Add(webBrowser1.Url.AbsoluteUri);
        }

        private async void Form1_Load(object sender, EventArgs e)
        {
            foreach (string link in fileList)
            {
                webBrowser1.Navigate(link);
                await Printed(link);
            }
        }

        private Task Printed(string link)
        {
            return Task.Factory.StartNew(() =>
            {
                while (!printedFileList.Contains(link))
                { }
            });
        }
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top