For Programmers: Free Programming Magazines  


Home > Archive > Microsoft Webservices > April 2006 > Re: Csharp async calls to Web Service that calls C++ :: blocking









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Csharp async calls to Web Service that calls C++ :: blocking
jeremiah johnson

2006-04-17, 7:07 pm

If you have the source code to the DLL, make sure the methods you're
calling aren't synchronized.

also, if the DLL has a method that calls another DLL somewhere that is
synchronized, you'll see the same effect.

You're describing synchronous behavior, so that what I'd say the cause is.

Brian Parker wrote:
> I inherited a C++ DLL that I need to remotely call multiple times
> asynchronously. What I have developed is:
>
> CSharp web application that makes asynchronous calls to a CSharp Web
> Service. The Web Service calls the DLL, which does some heavy
> processing, then sends the results back to the CSharp web application.
>
> The problem is that the calls to the DLL in the Web Service are blocking
> each other. If I send 10 async requests to the Web Service, debug
> statements show that they all reach the call to the DLL within 1/10 of a
> second, but only one call to the main method in the C++ DLL will run at
> a time. If I stub off the call to the DLL and replace it with something
> like a 15 second Sleep, the async Web Service calls all process
> asynchronously as expected.
>
> Is there an inherent reason why the DLL won't allow multiple calls to
> its main method to run concurrently? Does anyone have a suggestion as
> to why this is happening?
>
> -BEP

Brian Parker

2006-04-18, 7:09 pm

shumaker@cs.fsu.edu wrote:
> If the DLL is dealing with any objects that are thread safe, and they
> happen to try and access the same object for some reason(perhaps
> something static in the DLL that all threads would be sharing) then
> they will likely execute one at a time as they take they're turn with
> the object.


I've looked through the code and found a lot of static declarations in
methods. Almost all of them are utility methods that could be made
non-static. The other few, I'm not sure about but will look into.

I did find this in the code. Does it give any insight into the issue:

--CODE BEGIN-----------------------------------------
STDAPI DllCanUnloadNow(void)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState
());
return (AfxDllCanUnloadNow()==S_OK && _Module.GetLockCount()==0) ?
S_OK : S_FALSE;
}
--CODE END-------------------------------------------

Thanks to you guys for your suggestions so far.
-BEP
Brian Parker

2006-04-19, 7:08 pm

Mart wrote:
>
> Thanks. It's been a couple of years since I've done Visual C++ in
> anger, but here's where I'd probably start (sorry if I'm teaching
> grandma...):


Oops, I forgot. It was written in VC++ 6.0 but I made a very minor
change to some logic in it recently and recompiled in VStudio 2003. Not
sure if that makes a difference, but though it was worth a mention.

> 1 - How has the C++ been compiled? Is it multi threaded or single
> threaded?


The DLL itself does threading. Here's a quick overview of what it does:
I send an array to the DLL. The DLL hits a web page for each member of
the array and brings back the HTML. It parses the HTML, sticks it in a
database for my C# program to retrieve, then returns. For each web page
it hits and reads, it spawns a thread. I'm going to assume it was
originally compiled multi-threaded. How can I tell?

I did searches on LIBC, LIBCMT, and MSVCRT and turned up nothing. I
don't see where in the IDE I can control the linker options - only how
to set them via the command-line, a command file, or via the ENV vars.

> 2 - Is the C++ DLL COM or straight C++?


I'm really not sure.

> 3 - How, where and by what is the C++ DLL being loaded? Which process
> space is it running in? Does the model used allow multi threading or
> multiple loading?


This is what I have to do to use it:
I register the DLL using regsrv32
In VStudio, I go under "Project -> Add Reference" and find it under the
COM tab - that may answer your previous question.

> 4 - Does the C++ have any forced syncrohnisation in it - any clues like
> link syncs, mutexs, singleton pattern stuff etc?


It uses a few mutexes, but only when it accesses specific web pages, and
the 2 that use mutexes I'm not using in my testing so that code never
gets called.

One other place it uses mutexes is in the error handler. If there's an
error, it writes it to a log and the mutex is there to stop file errors.

> I'm not sure about the mechanics of how .NET uses native C++ stuff. Are
> there any restrictions in the way this works?


This is my first time with it. I'm not compiling the C++, I'm just
hooking into the DLL when my code runs. The DLL is already registered
in the system and waiting for something like my program to use it.

I don't think I gave you too many answers to your question, but
hopefully there's something there that may spark something. I
appreciate your help.

-BEP
Brian Parker

2006-04-20, 7:08 pm

<snip lots of good information from Mart>

I got it all figured out thanks to your hints and suggestions.

The DLL's project had been set to use a CComSingleThreadModel. I
changed this to CComMultiThreadModel in the header file and also changed
'Apartment' to 'Free' in the .rgs file. I wasn't sure if 'Free' was a
better choice than 'Both', though. It works, but still may not be correct.

Thanks for all the great information. Even though this is now working,
I'm still going to follow up on some of the things you suggested for
learning's sake.

-BEP
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com