0
Answer

Accessing printer in c# windows service fails

mgopinath

mgopinath

21y
2.2k
1
I am trying to print from a .net windows service when I call the Print() method of the PrintDocument class or do anything to access the printer such as to invoke the InstalledPrinters method of the PrinterSettings class, the service throws an error. The service is currently running under a domain user account and printers are installed on the machine for that user profile. Error logged in event log: Service cannot be started. System.TypeInitializationException: The type initializer for "System.Drawing.SafeNativeMethods" threw an exception. ---> System.Runtime.InteropServices.ExternalException: Failed to create system events window thread. at Microsoft.Win32.SystemEvents.EnsureSystemEvents(Boolean requireHandle, Boolean throwOnRefusal) at Microsoft.Win32.SystemEvents.InvokeOnEventsThread(Delegate method) at System.Drawing.SafeNativeMethods.Initialize() at System.Drawing.SafeNativeMethods..cctor() --- End of inner exception stack trace --- at System.Drawing.SafeNativeMethods.EnumPrinters(Int32 flags, String name, Int32 level, IntPtr pPrinterEnum, Int32 cbBuf, Int32[] pcbNeeded, Int32[] pcReturned) at System.Drawing.Printing.PrinterSettings.get_InstalledPrinters() at TestPrintService.Service1.OnStart(String[] args) in D:\Dev Projects\...\Sandbox\dotnetservice\TestPrintService\Service1.vb:line 65 at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state) Anybody please help me to resolve the issue. Thanks in Advance. Gopinath