我有一個Windows服務應打印出HTML文本。
操作系統是7,使用.NET 4.5.1和IE 11.Windows服務憑據
的代碼:
private static void PrintOnStaThread(string text)
{
using (WebBrowser webBrowser = new WebBrowser())
{
webBrowser.Navigate("about:blank");
webBrowser.Document.OpenNew(false);
while (webBrowser.DocumentText != text && webBrowser.ReadyState != WebBrowserReadyState.Complete)
{
webBrowser.Document.Write(text);
webBrowser.Refresh();
Application.DoEvents();
}
InternetExplorer internetExplorer = (InternetExplorer)webBrowser.ActiveXInstance;
internetExplorer.ExecWB(OLECMDID.OLECMDID_PRINT, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, Win32.PRINT_WAITFORCOMPLETION);
}
}
問題:
當服務使用記錄的帳戶Local System
的ExecWB
函數什麼都不做線程正在退出,沒有任何異常。當使用管理員憑據登錄服務時,ExecWB
函數完美工作,但我不想使用此帳戶。
任何意見將受到歡迎。
請勿在Windows服務中使用WinForms。問題很可能是由此造成的。使用不同的方式來打印HTML。如果您必須,請參閱[在C#中不使用打印對話框打印Windows Service中的html文檔](http://stackoverflow.com/questions/416314/print-html-document-from-windows-service-in-c-sharp-without -print-dialog),但仍然使用「WebBrowser」控件。另請參見[從Windows服務打印html文檔而不打印對話框](http://stackoverflow.com/questions/419412/print-html-document-from-windows-service-without-print-dialog)。 – CodeCaster 2014-10-03 13:01:10
我已經知道並在我的代碼中使用了第一個示例。我不明白「不要使用WinForms」是什麼意思。我不認爲我是。 – toy4fun 2014-10-03 13:43:52