Bonjour,
Je souhaite pouvoir imprimer des documents PDF depuis un programme C# (framework 4) exécuté en tant que service Windows.
Pour cela, j'utilise la méthode d'impression suivante (extrait de code) :
Ceci plante aléatoirement. Dans la pratique l'impression se déroule bien pendant 1 à plusieurs jours (on imprime environ 500 documents par jour), puis plante et quand le plantage est survenu, le redémarrage du service est nécessaire pour pouvoir imprimer de nouveau.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 LocalPrintServer printServer = new LocalPrintServer(); PrintQueue printQueue = printServer.GetPrintQueue( printerName ); PrintSystemJobInfo job = printQueue.AddJob( fileName ); job.JobStream.Write( data, 0, data.Length ); job.JobStream.Close();
J'ai deux types d'exception levées :
Espace insuffisant pour traiter cette commande à MS.Win32.UnsafeNativeMethods.CreateWindowEx(Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)
à MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32 exStyle, Int32 x, Int32 y, Int32 width, Int32 height, String name, IntPtr parent, HwndWrapperHook[] hooks)
à System.Windows.Threading.Dispatcher..ctor()
à System.Windows.Threading.Dispatcher.get_CurrentDispatcher()
à System.Printing.PrintServer.Initialize(String path, String[] propertiesFilter, PrinterDefaults printerDefaults)
à System.Printing.PrintServer..ctor()
à System.Printing.LocalPrintServer..ctor()
à SodisEDI.EDIOrderTerminate.PrintPDFDocument2(String printerName, String documentDirectory, String orderName) dans e:\DEV\KomotoTFS\Ketra\Main\Clients\Sodis\SodisEDI\SodisEDI\EDIOrderTerminate.cs:ligne 284
et
L’opération a réussi à MS.Win32.UnsafeNativeMethods.RegisterClassEx(WNDCLASSEX_D wc_d)
à MS.Win32.HwndWrapper..ctor(Int32 classStyle, Int32 style, Int32 exStyle, Int32 x, Int32 y, Int32 width, Int32 height, String name, IntPtr parent, HwndWrapperHook[] hooks)
à System.Windows.Threading.Dispatcher..ctor()
à System.Windows.Threading.Dispatcher.get_CurrentDispatcher()
à System.Printing.PrintServer.Initialize(String path, String[] propertiesFilter, PrinterDefaults printerDefaults)
à System.Printing.PrintServer..ctor()
à System.Printing.LocalPrintServer..ctor()
à SodisEDI.EDIOrderTerminate.PrintPDFDocument2(String printerName, String documentDirectory, String orderName) dans e:\DEV\KomotoTFS\Ketra\Main\Clients\Sodis\SodisEDI\SodisEDI\EDIOrderTerminate.cs:ligne 284
Microsoft indique bien qu'utiliser System.Printing n'est pas fiable dans le contexte d'un service windows (ceci est documenté dans la version 4.5 du framework).
Pour contourner le problème, j'ai tenté de lancer un petit exé charger de réaliser l'impression, cet exé étant lancé depuis le service, mais même plantage, après un certain temps.
Quel contournement est-il possible d'utiliser ?
Partager