自動保存用の PDF Creator セットアップであるデフォルトのプリンターに HTML レポートを一括印刷しようとしています。HTML ファイルを Internet Explorer 経由でロードし、そこからユーザー プロンプトなしで印刷します。
私が抱えている問題は、プログラムがループして HTML ファイルのリストを印刷するときに、一部のドキュメントがスプールされず、印刷されないことが判明したことです。これは while ループと Application.Dowork() を使用して解決できることをネットで読みました。これを実装すると、すべてのドキュメントが印刷されることがありましたが、これは改善されましたが、これはまれであり、確実な修正ではありませんでした。
私の問題は、処理が完了する前に各スレッドが閉じていることでしょうか?
もしそうなら、どうすればスレッドを独立して実行して、処理中にスレッドが閉じないようにすることができますか?
以下は、ドキュメントをデフォルトのプリンターに印刷するために使用しているコードです。
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);
}
}