Office Automation and Process Affinity
-
28-05-2021 - |
Pregunta
I am doing a bit of Office Automation and when printing a .doc file using VB.NET Office reference DLLs it causes great CPU load so I want to limit those processes to only use a single virtual core.
I haven't been able to find anything in Microsoft's documentation about this and I thought I would ask here as everyone is always so helpful.
This is the code I am using to print
Dim oWordApp As Word.Application
Dim oTargetDoc As Word.Document
oWordApp = New Word.Application
Select Case SQLdr("Priority")
Case 1
oWordApp.WordBasic.FilePrintSetup(Printer:=printPriority1, DoNotSetAsSysDefault:=1)
Case 2
oWordApp.WordBasic.FilePrintSetup(Printer:=printPriority2, DoNotSetAsSysDefault:=1)
Case 3
oWordApp.WordBasic.FilePrintSetup(Printer:=printPriority3, DoNotSetAsSysDefault:=1)
Case 4
oWordApp.WordBasic.FilePrintSetup(Printer:=printPriority4, DoNotSetAsSysDefault:=1)
Case 5
oWordApp.WordBasic.FilePrintSetup(Printer:=printPriority5, DoNotSetAsSysDefault:=1)
End Select
oTargetDoc = oWordApp.Documents.Open(SQLdr("DocumentName") & ".doc")
oWordApp.PrintOut()
oWordApp.Documents.Close()
oWordApp.Quit()
Solución
Here is how you can run this method in a Background thread: http://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx
ps the most likely reason your seeing high CPU is because of the document size, but you'll also see high memory because your not cleaning up your objects, eg:
Marshal.ReleaseComObject(app)
Here's an article to understand the best practice when it comes to native memory management via managed code: http://jake.ginnivan.net/vsto-com-interop