?
Solved

AfxBeginThread & Excel Automation

Posted on 2003-03-25
4
Medium Priority
?
244 Views
Last Modified: 2013-11-20
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
Comment
Question by:msinosic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 9

Expert Comment

by:Priyesh
ID: 8207707
try it from a user interface thread.
0
 

Author Comment

by:msinosic
ID: 8208217
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
 
LVL 48

Accepted Solution

by:
AlexFM earned 80 total points
ID: 8208441
I just guess: do you initialize COM in the start of your worker or user interface thread?
0
 

Author Comment

by:msinosic
ID: 8213817
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

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month10 days, 9 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question