-1

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()
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Rambomst
  • 653
  • 2
  • 10
  • 28
  • Why dont you just do it on a background thread? also this is client side automation isn't it? These days most people want to do the opposite and use all cores: http://msdn.microsoft.com/en-us/magazine/cc163340.aspx – Jeremy Thompson Apr 03 '12 at 03:19
  • In a perfect world it would be nice to allow it to run on a server by itself however there are several things on that server and when it becomes CPU hungry it messes with the servers performance. – Rambomst Apr 03 '12 at 04:11
  • I was actually warning you about it, I've memorized the KB article KB 257757 - dont ever do server-side automation of office, such a PITA. Unless your doing it with XML, but even that I'd be testing heavily. So what about me other idea of the background thread? – Jeremy Thompson Apr 03 '12 at 04:16
  • The way the process is started I have no clue how to do any process/thread management. – Rambomst Apr 03 '12 at 04:30

1 Answers1

1

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

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321