问题
I have to parallelize an application; This application has to process a file, I used tasks and each thread is processing one line of data (previously it used to process lines one by one sequentially). The problem is that the data provider uses some sort of caching and also it accesses the file multiple times during the process, so using one data provider was a hard work of making it thread-safe, instead I created on new data provider for each time a thread is processing a line. It works fine for the first couple of runs, but after some time the COM object which is used to access the file starts throwing the following error:
COMException occurred: ErrorCode = -2147287036
As I checked "2147287036" is the error code for "not enough resources to open another file". I'm sure I'm calling the close/dispose for the data providers/COM objects, so I have a hard time understanding why I can not access the file (I have an exception handling logic that tries to open the file in the write mode and if it is unsuccessful, it tries to open it in the read mode, and again if it is unsuccessful it throws the exception which is basically the one mentioned above).
My first clue is that the COM object doesn't release the file handle instantly. But still that doesn't make that much sense.
回答1:
Wouldn't it be better to have a producer/consumer setup where a single producer reads in the file line by line and feeds that into a queue where the consumer threads can go all out?
Opening the same file from multiple threads to do heavy crunching just sounds a bit cumbersome.
Sorry if I miss-understood your issue.
回答2:
I found the issue. There was a memory leak, in some parts of the code some copies of the COM object was being created and not released and apparently Marshal.ReleaseComObject(...) decrements the counter corresponding to the number of references to the COM object and only releases it if that number becomes 0.
来源:https://stackoverflow.com/questions/5246855/comexception-occurred-errorcode-2147287036