Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

AfxBeginThread & Excel Automation

Howdy All,

I am attempting to output a set of results into a pre-defined .XLS template file. This I have managed to achieve perfectly fine. However the process can take up to several minutes as there are alot of results to process. This halts the main thread for longer that I would like. The natural solution was to create a worker thread using AfxBeginThread, which I have done. Now all the Excel Automation is done within the worker thread function. However, all of the excel calls no longer work. ::Start() for example fails to start up excel, returns a failure and then my thread function promptly exits. I have even tried starting excel before the thread and then just using the ::Add() function calls inside the thread. Excel starts when I do this but the ::Add() function calls fail. I have come to the conclusion that Automating Excel is not possible from a worker thread. I do not call AfxBeginThread with any special parameters, simply with the name of the thread function and a NULL paramter to pass to the function. Is there something that I am missing? A setting that the worker thread should have? I assumed the thread would start with the same attributes as its parent thread and since that works that the worker thread should also work. Any help would be appreciated.

Thank you for your time,

Sino ... =)
0
msinosic
Asked:
msinosic
  • 2
1 Solution
 
PriyeshCommented:
try it from a user interface thread.
0
 
msinosicAuthor Commented:
Admitedly I was running out of time and rushing towards the end of my day but I did however follow the instructions to set up a user-interface thread as best as I could. When I tested this thread it also failed to correctly Start up Excel. The same problem as before in other words. Is there something special that needs to be set up in order to make it work? My thread class is inherited from CWinThread. I have used the DECLARE_DYNCREATE and IMPLEMENT_DYNCREATE macros. I know this is one of those simple problems, that's why its so difficult! Now, there is an oxymoron for you!

Thanks for your help,

Sino ... =)
0
 
AlexFMCommented:
I just guess: do you initialize COM in the start of your worker or user interface thread?
0
 
msinosicAuthor Commented:
In short, No I didn't.

Most of you will have one of the following at the start of your apps ...

HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

or

HRESULT hr = CoInitialize(NULL);

This is OK until you need to use COM objects in your worker/user-interface threads after which you should use

HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);

This however, caused my program to crash. The end result was to initialize COM from inside my user-interface thread using the line ...

HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

Now everything works ok!

Thanks for your help,

Sino ... =)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now