Link to home
Start Free TrialLog in
Avatar of doront99
doront99

asked on

Threads inside DLL

Dear Experts,

I'm an Adobe Acrobat plug-in developer, and I'm stack here with some problem related to DLLs, I would appreciate your help (with points of course... :) ).

The plug-in works as DLL.
Inside that DLL, I'm having a thread I'm creating for some kind of a message loop.
In that thread, when I get signaled, I'm running a function that I have that handles the message.
The problem I have is that in many time this function cause Acrobat to crash.
Now, I know the problem is with Acrobat but I don't have the source of it, and I have to bypass it.

When I'm trying to run the code directly and not inside the thread the code runs with no problems.

My question is: is there any difference in execution for a code that runs inside / outside a dll?

I'm working with Visual Studio 2003 .NET ("c++")
The thread is created with AfxBeginThread

Many thanks,
Doron
SOLUTION
Avatar of nonubik
nonubik

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of doront99
doront99

ASKER

here is an example
CString temp;
		
temp = "search.query(\"";
temp+="Test";
temp+="\", \"ActiveDoc\");";
 
AFExecuteThisScript(pdDoc,(LPCTSTR)temp,NULL);

Open in new window

BTW, I don't believe there is a synchronization problem since the thread has no option to handle any request before it finished.
SOLUTION
Avatar of DanRollins
DanRollins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have already the AFX_MANAGE_STATE MACRO on each function.

As for the second paragraph: how can I make the main thread to do something?
Let's say I did the calculation, now how can I tell the main thread to draw it on the screen?

>Let's say I did the calculation, now how can I tell the main thread to draw it on the screen?

You can define a custom window message that you can handle yourself in the main app window. And after you have done the calculation you may PostMessage(..) to your main window (i.e. handled in the main thread) and there handle the drawing.
I have tried that as well - hooking into the App class of the DLL with BEGIN_MESSAGE_MAP, but with no success.
It seems that the DLL doesn't actually listen to messages / there is no message loop running.

Does a DLL have some kind of a message loop that I can hook into?
My preferred method is to put a timer in the main window (or any window running on the main thread).   Lets say that you have it execute 4 times per second.  In most cases, that's plenty fast enough so that the user thinks he's seeing real-time updates.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you're still stuck here, let's see if we still can't help you! I think you need to describe your problem a little more in detail for us. After rereading everything, I still can't quite figure out exactly what your setup is here :-)

So, you are making a plugin for Adobe Acrobat. I'm assuming this is done through some well-defined interface Adobe provides for the purpose? Which I'm sure is documented somewhere, if you'll let us know exactly how you're interfacing with Acrobat. Is Acrobat calling your function through some menu command, your plugin starts, calculates something and then updates something on the screen? Or is it hooked into one or more messages in Acrobat, doing something every time a document view is updated e.g.? Or are you trying to get your dll start some calculation while the user is continuing working in Acrobat, wanting the results to be shown as soon as they are ready?

You make a dll. Does this have any user interface of it's own, or is it sending messages to Acrobat when it needs something drawn?

Why do you want a thread in your dll? It seems like you're having some lengthy calculation? Are you trying to achieve progress reporting during calculation? Or do you just want to have your dll wait for the calculation and then show the results?

If it's progress reporting you're looking for, you could have your main dll thread make a signal that you pass on to the worker thread doing the calculation. Then the main dll thread can do a WAIT for any signal from the worker thread (and for the worker thread to finish). The worker thread sets the signal every now and then, letting the main thread update the progress bar or whatever.

What does AFExecuteThisScript do? You haven't shown anything about how this starts the thread or how it calls into Acrobat? It seems like it somehow runs a script to search the current document for the phrase "Test", but I can't see why you want a thread for this? Isn't this just a message to Acrobat to perform a search task?

I hope this list of questions helps you understand what it is I don't understand :-)