I'm writing a multithreaded Win32 service for work that needs to call some COM objects. Basically what needs to happen is the main service thread sleeps until a timer object goes off, or it receives a service control message, including an external trigger. If the timer goes off or it receives this trigger, it needs to spring into action, which involves querying a database to find some assignments to run.
I'll interrupt the technical description for a moment to explain the point of this application. We're designing a web-based system that will serve binary files from a database (primarily PDF and Excel renderings of financial reports done by Cognos Impromptu). There's a web portal that the Finance people can go to to submit their reports to be run and converted to PDF and Excel, and another portal for the Execs to go to to view their reports. The service is supposed to query the DB to find which reports have been submitted, spawn the appropriate renderer (at this point, only Impromptu), generate the ouput file, and stick it back in the DB as a BLOB, to be pulled out and served by the Executive web portal.
Running a report through Impromptu involves hammering a DB halfway across the country across a narrow pipe (~256Kbps or so), and some of the reports take an awfully long time to run. Some of them take so long because they're spinning the DB so much, and some take so long because they're spewing so much data back through the pipe, but mostly the former. We'd like to run multiple reports at the same time, so assuming the pipe isn't saturated, the DB will be able to finish the shorter ones first, and our turnaround time will *appear* better to the Finance people (yes, the longer ones will just take longer to run, but there will be more "instant gratification" for the shorter ones).
Back to the main thread. So it hits our local DB to get a list of pending reports, and forks off one thread for each of them, so that it can go back to sleep like a good dispatcher (it's responsible for handling service stop requests, among other things, so it needs to finish its job quickly). Each of these worker threads hits the local DB some more to find pertinent information about the report (filenames, and so on), spawns an instance of Impromptu to run the report and generate some output files, and inserts these files into the local DB.
All local DB accesses are done through ADO (I couldn't find a good way to insert BLOBs in anything else), so the main thread needs COM initialized for that, and so do each of the worker threads. Of course, each thread has to spawn Impromptu (out of process, BTW), so it needs COM for that, too. No COM objects are passed between threads. Originally I passed a single ADO DB connection around to each thread, but I figured that was a Bad Idea, so I had each thread make a copy of it, so as to open its own connection.
When there is only one pending report in the DB, everything seems to work fine. When two reports are there, the two instances of Impromptu seem to step on each other. One of them may finish properly, but they'll eventually hang the service.
My question basically boils down to "How do I ensure that COM is using the correct threading model?" I'm calling CoInitialize(NULL) once in each independent thread, and no COM objects are being passed between threads. Should I initialize COM once at the beginning in some different thread model, or once for each thread in some different model, or what?
Ask questions if something isn't clear.
Liam
I'll interrupt the technical description for a moment to explain the point of this application. We're designing a web-based system that will serve binary files from a database (primarily PDF and Excel renderings of financial reports done by Cognos Impromptu). There's a web portal that the Finance people can go to to submit their reports to be run and converted to PDF and Excel, and another portal for the Execs to go to to view their reports. The service is supposed to query the DB to find which reports have been submitted, spawn the appropriate renderer (at this point, only Impromptu), generate the ouput file, and stick it back in the DB as a BLOB, to be pulled out and served by the Executive web portal.
Running a report through Impromptu involves hammering a DB halfway across the country across a narrow pipe (~256Kbps or so), and some of the reports take an awfully long time to run. Some of them take so long because they're spinning the DB so much, and some take so long because they're spewing so much data back through the pipe, but mostly the former. We'd like to run multiple reports at the same time, so assuming the pipe isn't saturated, the DB will be able to finish the shorter ones first, and our turnaround time will *appear* better to the Finance people (yes, the longer ones will just take longer to run, but there will be more "instant gratification" for the shorter ones).
Back to the main thread. So it hits our local DB to get a list of pending reports, and forks off one thread for each of them, so that it can go back to sleep like a good dispatcher (it's responsible for handling service stop requests, among other things, so it needs to finish its job quickly). Each of these worker threads hits the local DB some more to find pertinent information about the report (filenames, and so on), spawns an instance of Impromptu to run the report and generate some output files, and inserts these files into the local DB.
All local DB accesses are done through ADO (I couldn't find a good way to insert BLOBs in anything else), so the main thread needs COM initialized for that, and so do each of the worker threads. Of course, each thread has to spawn Impromptu (out of process, BTW), so it needs COM for that, too. No COM objects are passed between threads. Originally I passed a single ADO DB connection around to each thread, but I figured that was a Bad Idea, so I had each thread make a copy of it, so as to open its own connection.
When there is only one pending report in the DB, everything seems to work fine. When two reports are there, the two instances of Impromptu seem to step on each other. One of them may finish properly, but they'll eventually hang the service.
My question basically boils down to "How do I ensure that COM is using the correct threading model?" I'm calling CoInitialize(NULL) once in each independent thread, and no COM objects are being passed between threads. Should I initialize COM once at the beginning in some different thread model, or once for each thread in some different model, or what?
Ask questions if something isn't clear.
Liam