I’m having some problems with concurrency when using DLLImport, I have a Dll that provides some report I need to send over the web, so I have this:
[DllImport('Rep.dll', EntryPoint = 'PrintRep', CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] private static extern string PrintRep(several params...);
And over the Dll side there’s lots of variables and instances that then returns a path with the report.
The code I have now has a lock around the call of PrintRep, which works, but obviously makes pending requests wait, is there a way for this to work without a lock? Because if I take out the lock I get several ‘Attempt to write on protected memory’ errors and the Dll eventually hangs.
Is the report DLL thread safe? I’m going to guess no, since you said that if you remove the lock then you get errors. So, you have two choices, make the DLL threadsafe (requires source and changes to old dll), or serialize the requests to the DLL, which you’ve done. Those are your only two choices. However, do you need to make your users wait for the report right then? Can you, perhaps, maintain a list of users waiting for the report, and the parameters needed for the call, and then return the webpage to the user letting them know you’ll email them the report. Then you can process the reports in a serial manner while not causing your users to wait for the result.